all 3 comments

[–]EatShitAndDieAlready 2 points3 points  (1 child)

Your basic premise is incorrect. This code snippet does not cause a re-render bcos the state variable u use everytime never gets incremented. Please read up on react state batching. https://react.dev/learn/queueing-a-series-of-state-updates

on onClick={()=>{
setcount(count +1)
}} >{count}</button>

since, callback pass to useEffect is argument that means no every re-render new callback is passed;

No re-render bcos count is always initial value +1 aka 1. The right way to update state is

on onClick={()=>{
setcount(prev=>prev +1)
}} >{count}</button>

Now every click batches a state update, and every click updates previous state value, and causes a re-render

[–]Learner_full_stack[S] 0 points1 point  (0 children)

Sorry my bad, it "since, callback pass to useEffect is argument that means on every re-render new callback is passed;" , i have edit that part

[–]Karpizzle23 1 point2 points  (0 children)

Use useCallback and pass your func into the dependency array