Overwrite an integer state in reduction

This is another newbie to the redux . In my application, I would like to be able to load state from a text file, that is, to be able to completely reinitialize the entire state object. Therefore, I do not want to reset the state to the initial value, but replaced it with a new one. (The FYI application only stores data in the localStorage browser. Also, so far I have been following the awesome tutorial from http://redux.js.org/docs/introduction/index.html ) I tried several but none of them gave results. For example, in my reducers/index.js , I have:

 export default function App (state = {}, action) { return { todos: todos(state.todos, action), ... global: global(state, action) } } 

In reducers/global.js , I have:

 const global = (state = {}, action) => { switch(action.type) { case 'LOAD_DB_FROM_FILE': return action.fileContents; default: return state } } 

It happens that the state object, rather strange (or not :)), gets a new field called global that contains the original state (and not one read from the file), and it even becomes nested for a couple of levels (so I have a replica state in state.global.global. )

I realize that this approach is hacked, even ready to accept a fundamental flaw (due to my ignorance) in my installation, but I could not find a simple and unambiguous answer to my problem.

As always, any help would be greatly appreciated.

+5
source share
1 answer

I don't know much about redux, but based on what I know about JavaScript, I would say that you need something like this:

 // reducers/index.js export default function App (state = {}, action) { state = global(state, action); return { todos: todos(state.todos, action), ... }; } 

Thus, a reducer named global has the ability to immediately replace the entire state object.

+4
source

All Articles