james murphy
james murphy

Reputation: 1785

React native - Append to state object using setState

my state object looks like this:

activeUsers: {
    country: {userID:{userProfileObject}}
}

and I am trying to update state like this

this.setState(
    {
        activeUsers: {
            ...this.state.activeUsers,
            country: {userID: userProfileObject},
        },
    }
)

but it doesnt seem to work.....anyone have any idea why?

Upvotes: 0

Views: 188

Answers (1)

Ankit Patidar
Ankit Patidar

Reputation: 2781

You are trying to put state directly, instead of it you need to have key-value pair for resu

 this.setState(
    {
        activeUsers: {newData: {...this.state.activeUsers}, country: {userID: "12345"}},
    })

Here newData is new key where i put state value, you can make it anyway you want.

You can do it like this:

this.setState(
    {
        activeUsers: {country1: {...this.state.activeUsers.country}, country: {userID: "12345"}},
    }
)

Please let me know, if it worked for you. Thanks

Upvotes: 1

Related Questions