In this particular situation, if we pass an update function to setValue instead, it suffices to simply use useCallback with an empty dependency list. (Notably, useCallback((...) => ..., []) is equivalent to useRef((...) => ...).current.)
/* based on https://dev.to/anpos231/react-hooks-the-closure-hell-2-58g9 */importReact,{useState,useRef,memo}from"react";importReactDOMfrom"react-dom";import"./styles.css";lettimes=0;constExpensiveComponent=memo(({onClick})=>(<ponClick={onClick}>re-render: {times++}</p>));constApp=()=>{const[value,setValue]=useState(1);consthandleClick=useCallback(_=>{setValue(value=>value+1);},[]);return(<divclassName="app"><ExpensiveComponentonClick={handleClick}/><buttononClick={handleClick}>increment value</button><p>{value}</p></div>);};constrootElement=document.getElementById("root");ReactDOM.render(<App/>,rootElement);
In this particular situation, if we pass an update function to setValue instead, it suffices to simply use useCallback with an empty dependency list. (Notably,
useCallback((...) => ..., [])
is equivalent touseRef((...) => ...).current
.)So are you saying that we can function to setValue? And it will call it with the current state? Just like the old this.setState()?
How could I miss it!