I just created a custom hook that uses useState
and useEffect
internally.
When I import that hook into another React function component, call it ComponentA
, ComponentA
re-renders whenever the state in the custom hook changes.
Is it correct that ComponentA
should re-render, when hooks that it uses returns new values?
See comments in the code, for additional question clarifications.
Code:
const ComponentA = props => {
const myValue = useMyValue();
// COMMENTS:
// Whenever myValue returns a new value, ComponentA re-renders
// This in turn will cause the useMyValue() function to run.
// Seems unnatural with such a circular effect.
// Is my suspicion unfounded? Is this how it should work?
}
And as we know, state changes are one of the reasons why a component would re-render itself. This also applies to hooks - if the hook's state changes, the "host" component will re-render.
As we already saw before, React re-renders a component when you call the setState function to change the state (or the provided function from the useState hook in function components).
1. Memoization using useMemo() and UseCallback() Hooks. Memoization enables your code to re-render components only if there's a change in the props. With this technique, developers can avoid unnecessary renderings and reduce the computational load in applications.
Inspired by the concept of “partial application”, we can try to return a component with props that have been partially bound from a custom hook. However, the naive implementation will return new component definition when bound props changed, and that leads to unmount-remount behaviour that we may not expect.
A custom can be treated as a function simply which is executed from within the functional component and effectively the hooks that are present in the custom hook are transferred on to the component. So any change that would normally cause the component to re-render if the code within the custom hook was directly written within functional component will cause a re-render even if the hook is a custom hook.
Hooks are simple functions which can use other hooks and a function cannot return a value unless it is called, here if we are maintaining useState or useEffect inside custom hooks then on rendering the component first call custom hook (here useMyValue will be called), with the instance of componentA, which in turn call useState or useEffect hooks inside of it, with the same componentA instance, and return the intialized value to componentA.
const useMyValue = () => {
const [count, setCount] = useState(0);
// ...do something
return [count, setCount];
};
const ComponentA = (props) => {
const [value, setValue] = useMyValue();
};
now if are updating the value in componentA and we are calling setValue which has the reference of setCount from custom hooks then count got updated but useState will also update/ re-render the component for which it holds the instance, here componentA, and on re-rendering componentA will again call useMyValue hook which in turn call useState again and receive the updated value for count and finally return its updated value to the componentA.
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