wd and cc

-- Good good study, day day up!

Upgrade to React 16.3

#Javascript #React

随着 React native 升级,React 也升级到了 16.5 了。原来的改成新的生命周期了。

 1class ExampleComponent extends React.Component {
 2  static getDerivedStateFromProps(nextProps, prevState) {
 3    // Called after a component is instantiated or before it receives new props.
 4    // Return an object to update state in response to prop changes.
 5    // Return null to indicate no change to state.
 6  }
 7
 8  UNSAFE_componentWillMount() {
 9    // New name for componentWillMount()
10    // Indicates that this method can be unsafe for async rendering.
11    // Prefer componentDidMount() instead.
12  }
13
14  UNSAFE_componentWillUpdate(nextProps, nextState) {
15    // New name for componentWillUpdate()
16    // Indicates that this method can be unsafe for async rendering.
17    // Prefer componentDidUpdate() instead.
18  }
19
20  UNSAFE_componentWillReceiveProps(nextProps) {
21    // New name for componentWillReceiveProps()
22    // Indicates that this method can be unsafe for async rendering.
23    // Prefer static getDerivedStateFromProps() instead.
24  }
25}

React 在后面的版本里面,这几个方法都会被加上 UNSAFE_ ,直到被移除。

我们在 componentWillReceiveProps 里面主要是做了一个事情是根据后端反的数据来更新界面内容。因为我们用了 redux + saga,所以需要在这里做这个事情,如果是通过回调来更新数据的话,就不用这么麻烦了,直接在回调里面设置 state 就可以了。

因为在 getDerivedStateFromProps 里面不让接触现在的 this.props ,所以也不能简单的把原来 componentWillReceiveProps 的代码直接复制过来用。解决思路下面的链接都提到了很多,我自己总结有那几个。

和 prevState 做比较

比如想象一个页面有两个按钮,一个点了之后会 setState 为 test1 ,另一个按钮点了之后,会通过网络请求更新 store,然后更新 props 为 test2 ,那这个时候只需要和当前的 state 做比较就可以决定是不是要设置新的 state 了。

把 preProps 保存到 state 然后和 prevState 做比较

这么做基本就和原来使用 componentWillReceiveProps 基本一样了。没什么好说的了。

在 render 里面综合 state 和 props 的值

比如有时候页面显示的是用户录入和 props 的数据综合的,那可以在 render 里面做这个合并的工作。

comments powered by Disqus