Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

javascript - React useEffect missing dependecy?

enter image description here

Normally ? take a warning like that , React Hook useEffect has a missing dependency: 'loadAllAbout'. Either include it or remove the dependency array react-hooks/exhaustive-deps

I have code like that but when i give [loadAllAbout] also ? get a warning


Line 15:9: The 'loadAllAbout' function makes the dependencies of useEffect Hook (at line 13) change on every render. Move it inside the useEffect callback. Alternatively, wrap the definition of 'loadAllAbout' in its own useCallback() Hook react-hooks/exhaustive-deps


What i have to do ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What i have to do ?

Either one of the two things the error says, or just remove the function and put the logic that's in it directly into your useEffect callback, if you only ever use it from that callback.

Here's what the two options the error message gave you look like: Moving the loadAllAbout function into your useEffect callback:

useEffect(() => {
    // ...code using `loadAllAbout` (here or after it, either is fine)...

    function loadAllAbout() {
        // ...
    }
}, []);

Or using useCallback or similar to make loadAllAbout not a new function every time your component function runs:

const loadAllAbout = useCallback(() => {
    // ...
}, []);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...