生活随笔
收集整理的這篇文章主要介紹了
react --- 生命周期 给子组件传递数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
子組件
- /src/components/LifeCycle.js
import React
, { Component
} from 'react'export class LifeCycle extends Component {constructor(props
) {super(props
);console
.log("1.組件構建函數執行");}componentWillMount(){console
.log("2.組件將要掛載");}componentDidMount(){console
.log("3.組件已掛載");}componentWillReceiveProps(){console
.log("4.將要接收屬性傳遞");}shouldComponentUpdate(){console
.log("5.組件是否需要更新?");return true;}componentWillUpdate(){console
.log("6.組件將要更新");}componentDidUpdate(){console
.log("7.組件已更新")}componentWillUnmount(){console
.log("8.組件將要卸載");}render(){console
.log("x.組件渲染");return <div
>生命周期探究
</div
>}
}
父組件
import React
, { Component
} from 'react'
import { LifeCycle
} from './components/LifeCycle'class App extends Compoent{state
= {prop
:'some prop'},componentDedMount() {setTimeout(() =>{this.setState({ prop
: "a new prop" })}, 2000)}return (<div
><LifeCycle prop
={this.state
.prop
} </LifeCycle
></div
>)
}
總結
以上是生活随笔為你收集整理的react --- 生命周期 给子组件传递数据的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。