1

I have a 2D array and I need to update it in componentWillReceiveProps but it doesn't get updated.

This is my state;

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

And this is componentWillReceiveProps method;

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)
        }

    }

My nextProps come filled but I reckon I'm failing at setState.

Burak ULU
  • 305
  • 1
  • 6
  • 17
  • `this.setState({conditionRaw: rules.conditionRaw}, () => {console.log(this.state.conditionRaw);});` what does this say? – Nocebo Aug 10 '17 at 12:20
  • An empty array is returned @Nocebo – Burak ULU Aug 10 '17 at 12:21
  • 1
    Possible duplicate of [Why calling setState method doesn't mutate the state immediately?](https://stackoverflow.com/questions/42593202/why-calling-setstate-method-doesnt-mutate-the-state-immediately) – Mayank Shukla Aug 10 '17 at 12:26

1 Answers1

2

Your console log:

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

might be misleading, since setState() is asynchronous. Try this instead:

this.setState({conditionRaw: rules.conditionRaw}, () => {console.log(this.state.conditionRaw);});
Nocebo
  • 1,927
  • 3
  • 15
  • 26