How to cache API response and use it later in react & redux?

后端 未结 3 758
再見小時候
再見小時候 2021-02-05 17:47

In my React based application, there is a rest API call which fetches all the data in one shot which is needed for the whole page. The response has data which can be used in a p

3条回答
  •  攒了一身酷
    2021-02-05 18:24

    you can cache in HDD or RAM.

    • HDD = e.g. localStorage
    • RAM = application state, e.g. redux store.

    For localStorage you can use my little plugin for this: https://www.npmjs.com/package/localstorage-ttl

    In app state (RAM) - fire action to fetch data, use redux-thunk, redux-saga or similar to make a call and with reducer save data in the store. Retrieve data from store.

    https://github.com/gaearon/redux-thunk https://github.com/redux-saga/redux-saga

提交回复
热议问题