componentWillReceiveProps() is invoked before a mounted component receives new props. If you need to update the state in response to prop changes (for example, to reset it), you may compare this.props and nextProps and perform state transitions using this.setState() in this method.
Note that if a parent component causes your component to re-render, this method will be called even if props have not changed. Make sure to compare the current and next values if you only want to handle changes.
如果父组件导致子组件重新渲染,此时也会触发componentWillReceiveProps
这个函数(即使子组件的props
没有发生改变),因此,需要在componentWillReceiveProps
里面对this.props
和nextProps
进行比较,来决定是否执行操作。