0

I am trying to set a part of the state inside a reducer but I need to access part of the initial state that is nested.

What I am trying to do:

  return {
        ...state,
        list: action.payload,
        filter: action.params,
        filter.totalPages : action.totalPages,
      };

This however does not working, I have tried doing filter['totalPages'] and it also does not work. How and what is the best practice to do this?

Taylor Austin
  • 5,407
  • 15
  • 58
  • 103
  • What is the value of `filter.totalPages`? Do you really want to use that value as a property **name**? That would be an odd choice based on the name. What do you expect the result to be? Can you provide an example? – Felix Kling Oct 30 '17 at 20:20

3 Answers3

1

You should use spread operator for filter object too

  return {
    ...state,
    list: action.payload,
    filter: {
       ...action.params,
       totalPages: action.totalPages
    }
  };
Slawa Eremin
  • 5,264
  • 18
  • 28
1
return {
        ...state,
        list: action.payload,
        filter: action.params,
        [filter.totalPages] : action.totalPages,
      };

For more check Computed property keys in es6

Shishir Arora
  • 5,521
  • 4
  • 30
  • 35
1
return Object.assign({}, state, {
      ...state,
      {
        list: action.payload,
        filter: {
          ...action.params,
          totalPages: action.totalPages
        }
      }
  })

It's problably missing Object.assign check it out

Uğur Erdal
  • 67
  • 1
  • 12