- You must understand that if save state to the localStorage is an expensive so if your storage changing often you should think about performance. So I recommend to use lodash's
throttle
which won't allow launch your function if it's called more often than number in ms that you've given as second argument.
You should install lodash
via ``yarn:
yarn add lodash
- Or npm
npm i --save lodash
-
Note that you should wrap your calls to localStorage in try/catch because if user's browser doesn't allow caching in localStorage it will give an error and in this case you should return current state.
-
We add Root component because it helps to keep clean index.js and later if we need to add other wrapping components like Router, it will be easy to do.