S
S
sinevik2019-02-14 13:16:28
JavaScript
sinevik, 2019-02-14 13:16:28

REDUX. Is it possible to set one object property to (action.payload) and all others to null?

A question. Is it possible for a state redux object, when calling action. Change one field of the object in accordance with (action.payload), and set all other properties of the object to null.
For example:

case SET_VALUE_FIELD:
      return {
        ...state,
        [action.payload.field]: action.payload.value,
      };

Right now it changes one property, and copies all the rest from the previous state. Right here

..state,

Is it possible to somehow set all other properties equal to, for example, null?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-02-14
@sinevik

return {
  ...Object.keys(state).reduce((acc, n) => (acc[n] = null, acc), {}),
  [action.payload.field]: action.payload.value,
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question