以目前的實作方式,會送出很多請求


為了避免太多重複的請求,使用 lodash 的 memoize 來減少。
npm install -save lodash

然後修改 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})
})

請求和回應都被快取了,所以不會再送出重複的請求