#ActuallyAutistic web dev. Does front of the front-end. Loves perf and minimalism. Prefers HTML, CSS, Web Standards over JS, UX over DX. Hates div disease.
functionComponent(props){// can also be: const latest = useRef(props).current;const[latest]=useState(props);Object.assign(latest,props);// stable never changing callbacks that take `latest` as dependencyconst{hello,world}=useMemo(()=>{functionhello(){if(latest.active)latest.onClick();}functionworld(){if(latest.disabled)latest.onLifeHack();}return{hello,world};},[latest]);return<divdata-whatever/>;}
The benefit: as latest never changes (it always has the same object reference) you still do get access to the latest props. This pattern can be easier than writing a bunch of useCallback, especially if all of the callbacks can remain stable.
Sometimes it can also be useful that the callback changes when props change, but usually when dealing with things like event handlers you just want the latest prop without mutating the rendered callback that is already in DOM.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Maybe this is mindblowing to someone:
The benefit: as
latestnever changes (it always has the same object reference) you still do get access to the latest props. This pattern can be easier than writing a bunch ofuseCallback, especially if all of the callbacks can remain stable.Sometimes it can also be useful that the callback changes when props change, but usually when dealing with things like event handlers you just want the latest prop without mutating the rendered callback that is already in DOM.