Understanding dependency caching and referencing of useCallback in React
P粉044526217
P粉044526217 2023-08-15 19:29:37
0
1
452

Does the useCallback hook only cache the function reference or also the value/result of the function itself? Also, does using ref in a dependency array actually have any effect, like a component's ref? If not, is there a way to ensure that changes to the ref value have the appropriate effect?

I originally thought that only function references would be cached, but after reading an article I learned that useCallback(fn, deps) is equivalent to useMemo(() => fn, deps), and I'm not sure if this is the case This is actually the case. Also, I tried using the component's ref as a dependency (like Video.js and react-slick), but I think it doesn't have much impact compared to other dependencies.

P粉044526217
P粉044526217

reply all (1)
P粉170438285

Yes, the purpose ofuseCallbackis to allow a function to keep its reference between renders unless the dependencies you specify have changed.

For example, if you have a functionf(x,y)=>x y, you can use an empty dependency arrayuseCallback((x,y)=>x y,[ ]), this function will never change. It always produces consistent behavior because it only uses its arguments to parse the output. However, it may change if you have another functionhand another external valuez, andhis defined ash(x ,y)=>x y z, then you need to includezin the dependencies so that ifzchanges, the value returned fromuseCallbackThe function will have a new reference.

So the purpose ofuseCallbackis typically when you pass a function so it doesn't trigger a child component to re-render, or when you use a function as a function in a child component'suseEffectdeclaration dependencies. If the operations inside the function are expensive, thenuseCallbackis less useful and you should memorize the results separately.

Regarding therefthing, I think includingrefin the dependency doesn't do anything, it's like the array is empty. Maybe ifrefwas stored in the state it might be useful, but I'm not really sure.

Here is alink https://stackblitz.com/edit/stackblitz-starters-wwyw9f?file=src/App.tsx, which has some examples, which may be useful.

If it might be deleted, I can also paste it over.

import * as React from 'react'; import './style.css'; export default function App() { //x and y to be used as function arguments const [x, setX] = React.useState(0); const [y, setY] = React.useState(0); //z is variable also used in function calculation but not as an argument const [z, setZ] = React.useState(0); const ref = React.useRef(0); //counter to see how many times function updates //will start at 2 cause strict mode but that's no issue really const [i, setI] = React.useState(0); //function to add x and y from args and add z from state const fn = React.useCallback( (x: number, y: number) => { // console.log(`${x}+${y}`); return x + y + z; }, [z] // if you remove z and update it get wrong result ); //update fn count on fn change React.useEffect(() => { setI((i) => i + 1); }, [fn]); React.useEffect(() => { console.log('nice'); return () => console.log('ref cleanup'); }, [ref]); return ( 
{JSON.stringify({ x, y, z })}
x+y+z={fn(x, y)}
fnCount:{i}
); }

hope it is of help to you

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!