How do I implement caching in Redux?

前端 未结 6 2082
难免孤独
难免孤独 2020-12-24 12:17

I would like to avoid calling an API twice if I already have the data in my store.

How do I do this with Redux?

6条回答
  •  爱一瞬间的悲伤
    2020-12-24 12:55

    Don't reinvent caching, just leverage the HTTP cache.
    Your code should be practically unaware of the caching mechanism.
    Simply make the http request when you need the data, it doesn't matter if it is through redux thunks, promises, etc or directly without redux.
    The HTTP cache will do the caching for you.
    Of course for this to work you need to properly configure your server to set the appropriate caching parameters and validity.

提交回复
热议问题