React Native 监听属性值

有时我们在开发React Native项目时,需要监听属性值的变化,

那么我们就需要用到这个方法 componentWillReceiveProps(nextProps: IProps)

话不多说直接上代码

state = {
        modalVisible: false,
        hotName: this.props.currentselhotName, // 选中数据
        selIndex: this.props.data.indexOf(this.props.currentselhotName), // 选中索引
    };
    //  之所以在这里添加是因为当前页并不销毁,只是做显示隐藏,所以state的默认值只会走一次,所以需要监听属性变更时,重新变更state
    componentWillReceiveProps(nextProps: IProps) {
        if (nextProps.currentselhotName !== this.props.currentselhotName) {
            this.setState({
                selIndex: this.props.data.indexOf(nextProps.currentselhotName),
                hotName: nextProps.currentselhotName
            })
        }
    }
原文地址:https://www.cnblogs.com/lijianyi/p/14441890.html