P
P
Pogran2016-09-06 10:03:21
JavaScript
Pogran, 2016-09-06 10:03:21

How to change the code correctly?

I have this structure prntscr.com/ceowt6
And there is this code in the reducer

case DELETE_STRUCTURE_COUPLE:
      let obj_str = state.structure;
      objectPath.del(obj_str, action.level);

      return {
        ...state,
        structure: obj_str
      };

objectPath.del(obj_str, action.level); - removes data from the object by level. How can I write this code so that I don’t have to create an obj_str variable, but immediately change the structure here in return {...state, structure: ...}
Maybe someone will advise what literature on this topic, otherwise such problems often arise with various structures

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Risent Veber, 2016-09-06
@Pogran

You can for example like this:

case DELETE_STRUCTURE_COUPLE:
      return {
        ...state,
        structure: objectPath.del(state.structure, action.level)
      };

this requires objectPath.del to return the modified first argument,
but I don't think that would make the code any more readable. In general, use something like https://facebook.github.io/immutable-js/ so as not to change the previous state.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question