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?

1
  • 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? Commented Oct 30, 2017 at 20:20

3 Answers 3

1

You should use spread operator for filter object too

  return {
    ...state,
    list: action.payload,
    filter: {
       ...action.params,
       totalPages: action.totalPages
    }
  };
Sign up to request clarification or add additional context in comments.

2 Comments

I dont know why I didn't think of this, love this answer, will accept when I can.
1
return {
        ...state,
        list: action.payload,
        filter: action.params,
        [filter.totalPages] : action.totalPages,
      };

For more check Computed property keys in es6

Comments

1
return Object.assign({}, state, {
      ...state,
      {
        list: action.payload,
        filter: {
          ...action.params,
          totalPages: action.totalPages
        }
      }
  })

It's problably missing Object.assign check it out

2 Comments

Where is todos suddenly coming from? filter.totalPages : action.totalPages, is still invalid syntax.
I'm sorry. I fixed that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.