1

I need to save the data from the array to Saga (redux-saga). But I have an array that needs to maintain its sequence.

I add a new item in the action to which I pass "text" and "ID" after which I need to put a new item. As well as the reducer, which combines the first piece of the array before the index + new element + the second piece of the array after the index.

How can I save this array in local storage in saga, if the array is combined into a reducer, and saga is a middleware and is called before the reducer?

(Save is necessary in the Saga, it is a prerequisite, I know that this can be done by using the subscriber's)

The idea is that I can combine and save the array in the Saga, but then reducer will not do anything and I will transfer the processing status in middleware It's not very good What i need to write in saga?

reducer.js
  switch (action.type) {
      case ADD_NEW_ELEMENT:
       return [
          ...state.slice(0, action.afterIndex + 1),
          action.text,
          ...state.slice(action.afterIndex + 1)
       ];
      default:
       return state;
  }

action.js
       return {
          type: ADD_NEW_ELEMENT,
          text,
          afterIndex
       };
Daniel Guide
  • 11
  • 1
  • 3
  • Could you explain what you meant by `I need to save the data from the array to Saga`? You mean the state change should be accessible in the next saga call? That you can achieve by using select. `import {select} from 'redux-saga/effects'` – tkay Jul 09 '19 at 17:49
  • @tkay No, I need the data to be loaded from the localstorage when the component is first drawn and saved there every time it is changed – Daniel Guide Jul 10 '19 at 18:05

1 Answers1

0

usually for this case good solution is Redux Persist , https://github.com/rt2zz/redux-persist , https://www.npmjs.com/package/redux-persist ,

you choose reducer what you want, and redux-persist save and load it from localstorage for you :)

  • I know, but I'm not allowed to use any extensions other than redux-saga under the terms – Daniel Guide Jul 09 '19 at 17:56
  • may be this links can be help full for you https://twitter.com/dan_abramov/status/703684128416333825?lang=en https://stackoverflow.com/questions/35305661/where-to-write-to-localstorage-in-a-redux-app/35675304#35675304 https://egghead.io/lessons/javascript-redux-persisting-the-state-to-the-local-storage – Alexander Bryksin Jul 10 '19 at 07:34