1110
1110

Reputation: 6839

How to properly change state in reducer so component can receive update

In reducer I have:

if (action.type === 'remove_item') {
      delete state.itemsList[action.itemId];
      return {
        ...state
      }

But now componentWillReceiveProps(nextProps) can't see difference between:

this.props.items !== nextProps.items

I know that this happens because I changed state in reducer directly
but don't know how to propagate this change now so nextProps will have new state data?

Upvotes: 1

Views: 864

Answers (2)

sooper
sooper

Reputation: 6039

Try

...state.itemsList.filter((_, index) => index !== action.itemId)

So you're not mutating the original state, instead creating a new array.

Upvotes: 3

Codesingh
Codesingh

Reputation: 3384

It's not showing changes because state object is immutable and you are directly doing an operation on the immutable object.

Try this:

if (action.type === 'remove_item') {
      var list = state.itemsList.splice();
      list[action.itemId] = null
      return {
        ...state,
        itemsList: list,
      }

Cheers :)

Upvotes: 3

Related Questions