setState到2D数组 - ReactJS

时间:2017-08-10 12:18:00

标签: arrays reactjs collections

我有一个2D数组,我需要在componentWillReceiveProps中更新它,但它没有更新。

这是我的州;

this.state={
            conditionRaw:[[]]
        }

这是componentWillReceiveProps方法;

componentWillReceiveProps(nextProps){
        let rules = {conditionRaw:[[]]};
        if (nextProps.rootObject._id !== ""){
            rules["conditionRaw"] = nextProps.rootObject.conditionRaw
            this.setState({conditionRaw: rules.conditionRaw})
            console.log("DENEMEBİRLKİ", this.state.conditionRaw)
        }

    }

我的nextProps已经填满,但我认为我在setState失败了。

1 个答案:

答案 0 :(得分:1)

您的控制台日志:

console.log("DENEMEBİRLKİ", this.state.conditionRaw)

可能会产生误导,因为setState()是异步的。试试这个:

this.setState({conditionRaw: rules.conditionRaw}, () => {console.log(this.state.conditionRaw);});