I am a bit confused about what's the proper way to use useMemo. Reading here (Should I wrap every prop with useCallback or useMemo, when to use this hooks?) it says that I have to use it for expensive children, so what does that mean exactly? Can someone provide a real life example?
I created a new React project playing around with 2 different approaches.
You can check the full code here (in the same page you can switch from one method to the other):
https://codesandbox.io/s/lucid-rgb-fz60n
In this first example I pass to AppContext
an object containing userState
and setUserState
(logged or not).
const [userState, setUserState] = React.useReducer(reducer, initialUserState);
const appState = {
userState,
setUserState
}
<AppContext.Provider value={appState}>
Would this approach be acceptable?
In the second example, I use useMemo:
const [userState, setUserState] = React.useReducer(reducer, initialUserState);
const value = React.useMemo(() => {
return {
userState,
setUserState
};
}, [userState]);
<AppContext.Provider value={value}>
So I suppose that useMemo in this second case it's not a good choice since the children is not expensive. Is this correct?