Pallavi
Pallavi

Reputation: 77

How can we update the state of a dynamic object array, based on the index

I have initialized a dynamic object array like 'flag: {833: true, 834: false, 835: false, 836: false}' and want update the value based on 'index' (e.g. flag[834] = true) & setState with the updated value (e.g. this.setState({ watchTopicFlag[index] : true }); ) at run time. How can we get this?

Upvotes: 0

Views: 402

Answers (1)

BENARD Patrick
BENARD Patrick

Reputation: 30985

If your state is

this.state = {
    watchTopicFlag: {
        883: false,
        774: false,
        552: false
    }
}

You can do

this.setState({ watchTopicFlag: {...this.state.watchTopicFlag, 883: true}})

Upvotes: 1

Related Questions