react的生命周期(待更新)

293 阅读1分钟

1.父组件更新某值且该值为子组件的props ,则触发该函数。可以用来更新props,或者包装props然后更新到state

componentWillReceiveProps(newProps, oldProps) {
//在该函数(componentWillReceiveProps)中调用 this.setState() 将不会引起第二次渲染。
if (newProps.属性 !== lodProps.属性) {
  this.setState({
          属性:newProps.属性
      })
}
}

2.更新某值才重新渲染组件(用于优化性能,只在欧协值更新的时候才渲染组件,避免子组件重新渲染)

// 适用: 父组件更新时候,子组件的值并无更新且不需要重新渲染组件
shouldComponentUpdate(newProps) {
	if (newProps.someData !== this.props.someData){
    	 return  true
    }else {
    	return false
    }
}

3.清除定时器和手动创建的一些dom

componentWillUnmount() {
	// 清除任务
	Data.updateTask = null
    // 清除定时器
    if (this.timer) {
    	clearInterval(this.timer)
    }
}