S
S
Skandiy2020-04-22 18:28:38
JavaScript
Skandiy, 2020-04-22 18:28:38

How to make changes to the Redux state, by line with the path to change?

How to make changes to the state by disspatching a string with the path of what needs to be changed and the new value?
For example, there is such a state:

const initialState = {
  array: [
    {
      key: "value",
      obj: {
        key1: "value",
        key2: "value",
      },
      array: [
        {
          key: "value",
          obj: {
            key1: "value",
            key2: "value",
          },
        }
      ]
    },
    {
      key: "value",
      obj: {
        key1: "value",
        key2: "value",
      },
    },
    {
      key: "value",
      obj: {
        key1: "value",
        key2: "value",
      },
    },
  ],
  path: "",
  value: ""
};


And there is a dispatch action containing a string with the path "array[0].obj.key1" and a string with a new value "newValue"
dispatch(setNewData( "array[0].obj.key1", "newValue"))
Also a new value could be something like "obj: {key1: "newValue", key2: "newValue"}"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tervizator, 2020-04-22
@tervizator

Umm... I didn't really understand what should come out...
Like you dispatch an action that has type, and in the editor you have to process this type, namely in the reducer, in which your initialState is, and pass the old value and it is not needed in the action, because what for it is needed there if it is written in the global state

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question