What Are Hooks? ๐ช
It is an optimization technique. The memoization function caches the last return value and would return the cached value if the inputs are the same as the previous function call.
Hooks memoize and protect the variables and functions within the React component from being re-created on each re-render. Different hooks memoize different things.
That's the default behavior in class components, but we are trying to mimic that in function components!
useCallback
โ
This hook memoizes a function. It takes a callback function as param to memoize. Whenever the component re-renders, the useCallback function will be called again and again, and each time it'll return the exact same function reference, as long as the dependency doesn't change. So the returned function will remain same, as if it was defined outside the react-component.
function UserComponent({ user }) {
const userName = useCallback(
// ๐ This function is memoized
() => {
const name = user.name;
return `user: ${name}`;
},
[user],
);
}
In this example, UserComponent
component, which a javascript function can be called as many times as required as part of re-rendering the component, but if the user
prop doesn't change, then userName
function will remain exactly same. It will not be re-created.
But when the user
prop does change, then the useCallback
hook will return a brand new function, re-created new instance.
useMemo
โ
This hook has the exact same signature as useCallback
. Except, it memoizes a value, not the function. The value returned from the callback function is memoized, not the function itself. If it's dependencies don't change, it will not call the callback function it'll return the previous value which the hook has cached.
It will call the callback function for a fresh value only when the dependencies change
function UserComponent({ user }) {
const userName = useMemo(() => {
const name = user.name;
// ๐ This return value is memoized
return `user: ${name}`;
}, [user]);
}
In this example, useMemo will call the passed callback function on the first render and return the return-value of the callback function. Then whenever the component re-renders the useMemo function will be called to get the value for userName
, but the useMemo will not call the callback function if the dependency: user object hasn't changed, instead it will return the previous cached value.
useState
โ
It's a weird one. All other hooks memoize something that's passed to the hook function as param. But unlike other hooks, It's not memoizing the value passed to the useState function. It's memoizing the return value of the useState function. And the passed value is just the initial value to be memoized.
The memoized value doesn't change when some dependency (input) change, it doesn't even have a dependency array. Instead it gives us a dedicated function for changing the memoized value. And this function not only updates the memoized value but also triggers the re-render of the component.
function UserComponent({ user: usr }) {
const [
// ๐ This value is memoized
user,
setUser,
] = useState(usr);
}
useEffect
โ
This hook memoizes the side-effect. Yeah it sounds weird, basically, we're telling react, to run this callback anytime the dependency values change.
function UserComponent({ user }) {
useEffect(
// ๐ This function (side-effect) is memoized
() => doSomething(),
[user],
);
}