1. 程式人生 > >Redux和React

Redux和React

types ring bsp 其他 時也 執行 mount 按順序 都沒有

export app class Compo1 extends Component{

}

Compo1.propType = {
a:PropTypes.string,
fn:PropTypes.func.isRequired
}
限制父類組件輸入參數的屬性
isRequired 必輸

生命周期

組件從誕生到消亡,程序提供了各個階段:
1.上樹階段 Mounting,按順序調用下列方法:
1)constructor ()構造器
2)componentWillMount() 正在上樹
3)render () 顯示
4)componentDidMount()這個階段DOM已經上樹,可以對DOM做一些事情

上述四個階段沒有設置參數

2.更新階段 Updating
props和state的改變 在這個階段
1)componentWillReceriveProps(nextProps)
當父類傳遞prop時改變時發生
這個函數裏同時可以獲取老的和新的參數
新老交替時發生這個函數
2)shouldComponentUpdate(nextProps,nextState)
用於確認是否繼續執行Updating的其他生命周期的函數
不光父類改變,其本身改變時也能改變
return ture 時,才能繼續執行下去
這個函數可以獲取新的prop和state,
也能獲取到老的參數
3)componentWillUpdate(nextProps,nextState)
更新之前做的事情,此時視圖沒有變化,state和props都沒有變化
4)render()
5 )componentDidUpdate(prevProps,prevState)
更新之後做的事情,參數裏是老的參數
任何生命周期都能拿到老的和新的參數 只是放置的位置不一樣

3.下數階段Unmounting
componentWillUnmount()
將要下樹

Redux重要說明:
1)state reducer store 只有允許一組
2) actionCreator 返回action的函數

Redux和React