react 数据监听

时间:2025-01-18 16:01:04
监听组件传递的值:
 componentWillReceiveProps(newProps)
 {
 	参数为给组件传递的参数
 }
 
监听组件内部状态的变化:
componentDidUpdate(prevProps,prevState){
	参数分别为改变之前的数据状态对象
	if(prevState.属性名!=.属性名)
	{
		...
	}
}

代码示例:

    //组件接收新属性时调用
    componentWillReceiveProps(newProps)
    {
        const {searchName}=this.props;
        this.setState({
            loading:true
        })

        setTimeout(()=>{
            this.setState({
                loading:false,
                users:[{url:'/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1906469856,4113625838&fm=26&gp=','name':'jeff'},{url:'/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1906469856,4113625838&fm=26&gp=',name:'jeff2'}]
            })
        },2000)
    }