๐ฅ๐ฒ๐ฎ๐ฐ๐ ๐๐ผ๐ผ๐ธ๐ ๐๐ ๐ฝ๐น๐ฎ๐ถ๐ป๐ฒ๐ฑ
React provides powerful hooks to optimize performance and manage navigation. You can use useMemo, useCallback, and useNavigate to improve your applications.
- useMemo: Memoizes a calculated value, improving performance and avoiding unnecessary recalculations.
- useCallback: Memoizes a function, preventing unnecessary recreation and improving performance.
- useNavigate: Enables programmatic navigation between routes.
Here's how you can use them:
- useMemo: Use it for expensive calculations, like calculating a value that depends on other values.
- useCallback: Use it when passing functions to child components, to prevent unnecessary re-renders.
- useNavigate: Use it to navigate to different pages, redirect users after login, or navigate back and forward.
For example, you can use useMemo to calculate a squared value:
const squaredValue = useMemo(() => {
return count * count;
}, [count]);
You can use useCallback to memoize a function:
const handleClick = useCallback(() => {
console.log("Button Clicked");
}, []);
You can use useNavigate to navigate to a different page:
const navigate = useNavigate();
navigate("/about");
These hooks help you build efficient and user-friendly applications. Source: https://dev.to/jayashree_a84b6eff7bc414e/react-hooks-explained-usememo-usecallback-and-usenavigate-34f4