今天看下 Switch 开关组件 ,学习组件设计 以及一些知识
- react.PureComponent react15.3中 新增的组件 看名称可知 是纯组件意思 相比于 常用的react.Component 他能减少render 次数 自动的将更改后的state 和Props 与之前的做浅比较,如果是引用数据类型的话 是根本引用地址是否改变 来判断是否render
- shouldComponentUpdate 原组件只要state props变化了 执行到 改声明周期函数默认都是返回true 在纯组件中,如果有shouldComponentUpdate 就以他返回结果 决定是否重新render,但是没有再去自动做浅比较 决定是否render
- forceUpdate() 强制render 此时不会触发 shouldComponentUpdate 好理解 都强制了 这个触发自然是无意义的
- getDerivedStateFromProps 静态函数 做props 和state的映射 react16.4 以后 setState forceUpdate 会执行这个方法