Sid
Sid

Reputation: 523

Why does react redux does not re render view for object states changes?

New to redux and react and I am not sure why views are not updated when objects in state is updated.

See jsbin: https://jsbin.com/wucutuhovu/1/edit?js,output

However it works for numbers: https://jsbin.com/kaloqanise/1/edit?js,output

Upvotes: 0

Views: 118

Answers (2)

Ademola Adegbuyi
Ademola Adegbuyi

Reputation: 1054

Avoid changing the state directly. I edited your first code with the objects. The second one is right but let's say we have different states in our app, so we have to initialize our states as an object. Check out the code here. Since you're using ES015, you should be able to grab some of those changes.

Codes Changed: I removed your JSON.stringify, created variable "INIT_STATE"

Upvotes: 1

Gilad Artzi
Gilad Artzi

Reputation: 3084

Redux is doing a shallow comparison to the state. When you change state.value, the state still points to the same object.

Try changing state.value += 1 to:

return Object.assign({}, state, {
    value: state.value + 1
});

Check out this JSBin: https://jsbin.com/cikinocudo/1/edit?js,output

Upvotes: 3

Related Questions