react 数据监听

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

代码示例:

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

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

你可能感兴趣的:(React,react)