How can I avoid re-hitting to API after returning response from the same API under that same parameter
How can I use useMemo hook with axios part for better performance as I am new in react and did studied that useMemo can be used for performance optimization.
Edited:
the code with memo function now it this a right way?
const initialState = {
SwichManagment: [],
Error: false
}
const reducer = (state, action) => {
switch (action.type) {
case "FETCHREQUEST":
return {
...state,
hasError: false
};
case "FETCHSUCCESS":
return {
...state,
SwichManagment: action.payload,
};
case "FETCHFAILURE":
return {
hasError: true
};
default:
return state;
}
}
const SwitchManagment = ({ IdentifierId }) => {
const [states, dispatch] = useReducer(reducer, initialState)
const memo = (callback) => {
const cache = new Map();
return (...args) => {
const selector = JSON.stringify(args);
if (cache.has(selector)) return cache.get(selector);
const value = callback(...args);
cache.set(selector, value);
return value;
};
};
const memoizedAxiosGet = memo(axios.get);
useEffect(() => {
dispatch({
type: "FETCHREQUEST"
});
memoizedAxiosGet(`https://example.com/${IdentifierId}/now`)
.then(reponse => {
dispatch({
type: "FETCHSUCCESS",
payload: response.data
});
})
.catch(error => {
dispatch({
type: "FETCHFAILURE"
});
});
}, [IdentifierId]);
return (
<div >
{Object.keys(states.SwitchManagment).map(key => (
<div className="item" key={states.SwitchManagment[key].id}>
<p>{states.SwitchManagment[key].name}</p>
<p>
{states.SwitchManagment[key].progress}
</p>
</div>
))}
</div>
);
};
React.useMemo does not save values from previous results. Check this example at dev.to:
const value = useMemo(() => expensiveFunction(a), [a]);
When it already did the calculation for
abeing2, then it won't do it for2next time. Likewise for the3or4.However, it really can only memoize one value. If the first time, it did the calculation for
1, and next it did the calculation for2, it won't remember what the result is for1any more. When supplied with1again, it will do the calculation again.
So in your case, you should create a memo function with long memory:
const memo = (callback) => {
// We will save the key-value pairs in the following variable. It will be our cache storage
const cache = new Map();
return (...args) => {
// The key will be used to identify the different arguments combination. Same arguments means same key
const key = JSON.stringify(args);
// If the cache storage has the key we are looking for, return the previously stored value
if (cache.has(key)) return cache.get(key);
// If the key is new, call the function (in your case axios.get)
const value = callback(...args);
// And save the new key-value pair to the cache
cache.set(key, value);
return value;
};
};
const memoizedAxiosGet = memo(axios.get);
This memo function will act like a key-value cache. If the params (in your case your URL) of your function (axios.get) are the same, the function will not be executed. Instead, the previous result will be returned.
So you can just use this memoized version memoizedAxiosGet in your useEffect to make sure network request are not repeated for that particular petition.
Check this demo integrated in React!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With