React Hooks - 2024
React Hooks - 2024
called hooks
The difference between normal functions and hooks is that
you can only call the hooks at the top level of your
component.
1. useRef
Usage: useRef is a react hook that lets us in referencing a value
that is not needed for rendering. This help us to store a value
which is not required for visual display. Example: In a stopwatch
we need useState to show the change time but we also need an
interval id so that we can stop the interval on button press.
Q. When does the child component re-render and how to avoid its re
rendering?
-> By default, all child components re-render automatically when the state of a
parent component changes. This includes even the child components that
weren’t affected by the change. Although re-rendering is not by itself
noticeable to the user (you shouldn’t actively try to avoid it!), you might want
to skip re-rendering a part of the tree that clearly wasn’t affected by it for
performance reasons. Immutability makes it very cheap for components to
compare whether their data has changed or not.
To avoid the child components from re rendering React uses memo.