By current implementation, many requests are sent


In order to prevent too many redundant requests, use lodash memoize to reduce.
npm install -save lodash

Then change src/actions/index.js
import _ from 'lodash'

// assign fetchUser to memoize, so that function instance is the same
export const fetchUser = (userId) => dispatch => _fetchUser(userId, dispatch);

// define a function to adapt memoize
const _fetchUser = _.memoize(async (userId, dispatch) => {
    const response = await jsonPlaceholder.get(`/users/${userId}`)

    dispatch({type:'FETCH_USER', payload: response.data})
})

request and response are cached, so no redundant request will be sent


What to read next

Previous / Next
Previous: Redux - 避免發送太多請求 Next: Redux - 用 Redux Thunk 避免重複請求
More like this
Subscribe via RSS: Macro · Tech