React生命周期函数的正确使用

  1. 当一个页面中存在父子组件时需要注意使用componentWillMount和componentDidMount这两个生命周期函数,如果需要先加载父组件里面数据传递给子组件,然后再加载子组件(获取网络数据)这种情况下我们不能同时在子父组件中都是用componentDidMount来获取数据,因为这个时候会先执行子组件的componentDidMount生命周期函数,但是此时还未得到从父组件传递过来的数据而报错。解决方案有两种:a.父组件使用componentWillMount子组件使用componentDidMount;b.父组件使用componentWillMount子组件也是用componentWillMount即可
  2. 页面中如果要实现联动效果的时候,比如a页面包含b1左页面和b2右页面,点击b1中的按钮b2页面内容对应变化,b1向b2通过redux传参,b2首次通过componentDidMount接收,后来通过componentWillReceiveProps接收

猜你喜欢

转载自blog.csdn.net/Mrs_dai/article/details/86668294