Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Custom React Hooks Cause Re-Renders of Dependent Components?

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?
}
like image 586
Magnus Avatar asked May 28 '19 15:05

Magnus


People also ask

Do custom hooks cause re-render?

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.

Which React hooks can cause a component to 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).

Which React hook prevents unnecessary re-rendering of a component?

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.

Can custom hooks return components?

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.


Video Answer


2 Answers

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.

like image 116
Shubham Khatri Avatar answered Oct 08 '22 04:10

Shubham Khatri


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.

like image 1
Krishna Agarwal Avatar answered Oct 08 '22 02:10

Krishna Agarwal