In the code below, even if {useCallback} is not used for the {countUp} and {countDown} functions, from the {React.memo} point of view in the {AppTwo} component receiving the two functions as props, even though the two functions are newly created, the old Since it is equal to the value of , isn't it possible to prevent re-rendering? Why is it re-rendering?
function App(){
const [count,setCount]=useState(0);
function countUp(){
setCount(count=>count+1);
}
function countDown(){
setCount(count=>count-1);
}
return(
<div>
<p>count:{count}</p>
<AppTwo countUp={countUp} countDown={countDown}/>
</div>
)
}
const AppTwo=React.memo(
({countUp, countDown})=>{
useEffect(()=>{
console.log('re-rendering.')
})
return(
<div>
<button onClick={countUp}>count UP</button>
<button onClick={countDown}>count DOWN</button>
</div>
)
})
By providing an empty dependency array to useEffect in AppTwo, you ensure that the effect is only run once, effectively avoiding unnecessary re-renders caused by the effect itself.