Skip to content

Latest commit

 

History

History
28 lines (22 loc) · 707 Bytes

Redux.MD

File metadata and controls

28 lines (22 loc) · 707 Bytes

Create react app with redux and typescript

npx create-react-app my-app --template redux-typescript

Persisted state @ subscribe

https://stackoverflow.com/questions/35305661/where-to-write-to-localstorage-in-a-redux-app

//on subscribe
store.subscribe(()=>{
  localStorage.setItem('reduxState', JSON.stringify(store.getState()))
});

//before creating the store, check local storage
const persistedState = localStorage.getItem('reduxState') 
                       ? JSON.parse(localStorage.getItem('reduxState'))
                       : {}

//Pass persisted state to store
const store = createStore(
  reducer, 
  persistedState,
  /* any middleware... */
)