0

I have a state of an array like this:

constructor(props){
   super(props)
   this.state={
      test_steps: []
   }
}

and it holds the following properties:

test_steps: [
    {
        description: "Test desc"
        expected_results: "Test exp. results"
        other_info: "test other info"
        status: true
        step_number: "1"
        _id: {$oid: "1234565894"}
    }
]

If I wanted to update only **other_info** property on this state, how will I achieve that?

1

2 Answers 2

1

You first need to get the your state and store it in variable. Then you loop the value and modify it. After that just set the state.

let test_steps = this.state.test_steps;
    test_steps.map((test)=>{
    test.other_info = "YOUR_UPDATED_INFO";
    return test;
    });

this.setState({test_steps});
Sign up to request clarification or add additional context in comments.

Comments

1

Since there is one element in your array you can use spread syntax, then change the related property.

this.setState(prevState => ({
      test_steps: [
        {
          ...prevState.test_steps[0],
          other_info: "foo"
        }
      ]
}));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.