Today i practised three important hooks which are advanced hooks.
- useMemo()
- useCallback()
- useRef()
1.What is useMemo()?
- It is a React Hook that remembers a calculated value.
- It helps avoid unnecessary recalculations when a component re-renders.
Syntax:
const remembered value = useMemo(() =>{
return calculation;
},[dependencies];
import { useState, useMemo } from "react";
function MemoHook() {
const [count, setCount] = useState(0);
const [age, setAge] = useState(0);
function changeAge(num) {
for (let i = 0; i < 1000000; i++) {
// Expensive calculation
}
return age + num;
}
const chAge = useMemo(() => changeAge(10), [age]);
return (
<div>
<h1>Count: {count}</h1>
<h1>Age: {age}</h1>
<h2>Calculated Age: {chAge}</h2>
<button onClick={() => setCount(count + 1)}>
Count +
</button>
<button onClick={() => setAge(age + 1)}>
Age +
</button>
</div>
);
}
export default MemoHook;
Workflow process:
when the component renders initially the callculation runs.
when count changes the previous calculated value is reused.
when age changes the calculation runs again because age is the dependency array.
Important:useMemo() returns a calculated value not a function.
2.What is useCallback()?
useCallback() is a React Hook that memorizes a function.It helps preserve the same function reference between renders when dependencies have not changed.
Syntax:
const rememberedFunction = useCallback() => {
//Function logic
},[dependencies]);
Example:
import { useRef, useState } from "react";
function CallbackHook() {
const [discount, setDiscount] = useState(10);
const prevDiscount = useRef(discount);
console.log(discount, prevDiscount.current);
prevDiscount.current = discount;
return (
<div>
<h1>Discount: {discount}</h1>
<button onClick={() => setDiscount(discount + 5)}>
Increase Discount
</button>
</div>
);
}
export default CallbackHook;
useCallback() remembers the function.
when the component re-renders,React uses the function while its dependencies remain unchanged.
The empty dependency array means the function does not depend on changing react values.
Important:useCallback() returns a function where useMemo() returns a callculated value.
3.what is useRef()?
It is a ReactHook used to store a mutable value.
Unlike useState() changing a ref does not cause a component to rerender.
Syntax:
const myRef = useRef(initialvalue);
access or update the stored value using:
myRef.current
Example:
import { useRef, useState } from "react";
function CallbackHook() {
const [discount, setDiscount] = useState(10);
const prevDiscount = useRef(discount);
console.log(discount, prevDiscount.current);
prevDiscount.current = discount;
return (
<div>
<h1>Discount: {discount}</h1>
<button onClick={() => setDiscount(discount + 5)}>
Increase Discount
</button>
</div>
);
}
export default CallbackHook;
_
Example:_
useRef() stores a value in its.current property withput triggering another render.
4.Difference :
useMemo() remembers a calculated value.
useCallback() remembers a function.
useRef() Stores a mutable value between renders.
5.Conclusion:
By practising these hooks i learnt.
- to use memoization in React.
- to avoid unnecessary calculation with useMemo().
- to preserve function references with useCallback().
- to store values using useRef().
- The diffrence between useMemo(),useCallback(),useRef().
Top comments (0)