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

后端 未结 3 762
再見小時候
再見小時候 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:25

    You could choose a redux-cached-api-middleware (disclaimer: I'm the author of this library), that's especially built for such cases.

    Here's an example that might fit your case:

    import React from 'react';
    import PropTypes from 'prop-types';
    import { connect } from 'react-redux';
    import api from 'redux-cached-api-middleware';
    import Dropdown from './Dropdown';
    import Error from './Error';
    
    class ExampleApp extends React.Component {
      componentDidMount() {
        this.props.fetchData();
      }
    
      render() {
        const { result } = this.props;
        if (!result) return null;
        if (result.fetching) return 
    Loading...
    ; if (result.error) return ; if (result.successPayload) return ; return
    No items
    ; } } ExampleApp.propTypes = { fetchData: PropTypes.func.isRequired, result: PropTypes.shape({}), }; const CACHE_KEY = 'GET/items'; const enhance = connect( state => ({ result: api.selectors.getResult(state, CACHE_KEY), }), dispatch => ({ fetchData() { return dispatch( api.actions.invoke({ method: 'GET', headers: { Accept: 'application/json' }, endpoint: 'https://my-api.com/items/', cache: { key: CACHE_KEY, strategy: api.cache .get(api.constants.SIMPLE_SUCCESS) .buildStrategy(), }, }) ); }, }) ); export default enhance(ExampleApp);

    It will fetch from API only once, and whenever you call fetchData it will return data from the cache, even if the call is from another component.

    The setup for this library is as follows:

    import { createStore, combineReducers, applyMiddleware } from 'redux';
    import thunk from 'redux-thunk';
    import { apiMiddleware } from 'redux-api-middleware';
    import api from 'redux-cached-api-middleware';
    import reducers from './reducers';
    
    const store = createStore(
      combineReducers({
        ...reducers,
        [api.constants.NAME]: api.reducer,
      }),
      applyMiddleware(thunk, apiMiddleware)
    );
    

    With this approach after user re-loads the page all redux state will be lost, if you'd like to save that state you could choose a redux-persist library to sync the state to localStorage and use would probably use different caching strategy for redux-cached-api-middleware.

提交回复
热议问题