react 子组件监听props 变化

componentWillReceiveProps //已经被废弃  


componentDidUpdate  //推荐使用
getDerivedStateFromProps// 推荐使用



componentDidUpdate(prevProps){
        if(prevProps.item.width!==this.state.width){
            this.setState({
                width:prevProps.item.width,
            })
        }
    }


//如果条件不存在必须要返回null   
static  getDerivedStateFromProps(props, current_state) {
        if(props.item.width!==current_state.width){
            return {
                
                width:props.item.width,
            }
        }
        return null
}
componentWillReceiveProps //已经被废弃 componentDidUpdate //推荐使用 getDerivedStateFromProps// 推荐使用 componentDidUpdate(prevProps){ if(prevProps.item.width!==this.state.width){ this.setState({ width:prevProps.item.width, }) } } //如果条件不存在必须要返回null static getDerivedStateFromProps(props, current_state) { if(props.item.width!==current_state.width){ return { width:props.item.width, } } return null }
经验分享 程序员 微信小程序 职场和发展