javascript - is Updating reducer initial state with localStorage data a good practice? -


i have reducer initial state , working fine.

const initialstate = [items:{     id: 1,     text: 'monday', }, {   id: 1,     text: 'tuesday', }];  function daychaged(state = initialstate, change) {  .. } 

my app working fine. however, added support local storage. want store new item in storage whenever user add one. , when page refresh/reload, want set initialstate value of initialstate plus localstorage values. @ moment added in state/reducer class , however, feel not best practice.

const storageitems = json.parse(localstorage.getitem('items')); if(storageitems) {     initialstate = { todos: initialstate.items.concat(storageitems) }; }   function daychaged(state = initialstate, change) {      ..     } 

its working fine , doing want. however, complicate reducer understanding. please should add such feature anc considering intention, how integrate without affecting testability of state?

first off consider if need change initialstate of reducer or perhaps need setting initial state of entire store (in case initial state of individual reducers ignored).

see signature of createstore function, accepts initial store state second (optional) argument, pass state localstorage there , should golden.

https://github.com/reactjs/redux/blob/master/docs/api/createstore.md


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -