Last active
April 24, 2018 07:31
-
-
Save jurosh/30f85da26f4177bb0b7c220ea74444e6 to your computer and use it in GitHub Desktop.
Redux Middleware: Simple local storage single item cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const cacheMiddleware = store => next => action => { | |
| if(action.type !== 'GET_SOMETHING') { | |
| return next(action); | |
| } | |
| const data = getFromLocalstorage() | |
| if(!data) { | |
| // Fetch and put to localstorate for later use | |
| const data = fetchFromServer() | |
| return next({ type: 'SERVER_RESULT', data }) | |
| } | |
| return next({ type: 'CACHED_RESULT', data }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment