This is a Next.js project.
- Creates a new version of the component
- Compare it with the older version
- If there's any changes, it will update what's changed
- Prevents the React to create a new component if the properties received are equal using shallow compare (referential equality)
- Therefore it will work perfectly if is an number or string, but when is an object it's needed to pass an function to change comparation method
- In Pure function components
- Components that renders too often
- The component re-renders with the same props
- When the component is a medium to large size
- Avoid when something that use too much processing power like some calculation that it's called multiple times
- The first param it's the function that return a value
- The second it's and array with an dependencie like in
useEffect
, only when the variable that is in the array changes it will recalculate - It's also used for referential equality
- Heavy calculations
- Referential equality (When the information is repassed to a children)
- Is only used for referential equality (Verify if is the same thing on the memory) when an function is passthru an children, the React will verify if is the same thing on the memory.
- So if a parent component re-renders and there's no useCallback the function will use another place on memory and the children will re-renders also. But if useCallback is used it keeps the function in the same space in the memory and it will not render the children.
First, run the development server:
npm run dev
# or
yarn dev
and the json server:
npm run server
# or
yarn server
Open http://localhost:3000 with your browser to see the result.