0

am learning useEffect and the way it works.

For now I am passing a function into my useEffect. After this I get the message:

React Hook useEffect has a missing dependency: 'handleSearch'. Either include it or remove the dependency array.

I tried to wrap everything into a useCallback, but then the app just crashes. I suuspect, that useEffect is rerendering my function on every change, so I would like to prevent it. Hope that someone can help me.

Here is some of my code:

const App = () => {
    const API_KEY = process.env.REACT_APP_API_KEY;

    const [ videos, setVideos ] = useState([]);

    const [ searchedValue, setSearchedValue ] = useState({
        selectedVideo: null
    });

    const handleSelectedVideo = (singleRenderedVideo) => {
        setSearchedValue((previous) => ({
            ...previous,
            selectedVideo: singleRenderedVideo
        }));
    };

    const handleSearch = async (inputText) => {
        const response = await youtube.get("/search", {
            params: {
                q: inputText,
                part: "snippet",
                type: "video",
                maxResults: 16,
                key: API_KEY
            }
        });

        setVideos(response.data.items);
        setSearchedValue({
            selectedVideo: response.data.items[0] //take the first search result and make it appear as a playable one
        });
    };

    useEffect(() => {
        handleSearch();
    }, []);



    return (
        <div>
            Hello
        </div>
    );
};

export default App;

I tried to deal with it with useCallback, but it didnt help, just got a 403:

const handleSearch = useCallback(
    async (inputText) => {
        const response = await youtube.get("/search", {
            params: {
                q: inputText,
                part: "snippet",
                type: "video",
                maxResults: 16,
                key: API_KEY
            }
        });

        setVideos(response.data.items);
        setSearchedValue({
            selectedVideo: response.data.items[0] //take the first search result and make it appear as a playable one
        });
    },
    [ API_KEY ]
);

useEffect(
    () => {
        handleSearch();
    },
    [ handleSearch ]
);

Why should I include (according to Eslint) API_KEY as dependency if it never changes?

I tried to mockup the code in Sandbox. Sorry, that it is not working because of the API_KEY: CODESANDBOX

Drew Reese
  • 165,259
  • 14
  • 153
  • 181
  • Cus eslint is pretty dump. I don’t event turn on that rule. But again, it’s a trade-off. Machines can enumerate better than human, they cannot reason. – hackape Sep 03 '20 at 04:54
  • But what do you wanna solve as the problem though? Besides the eslint warning which you can ignore. – hackape Sep 03 '20 at 05:05
  • If nothing else calls `handleSearch` just define it ***in*** the effect hook. Does this answer your question? [React Hook Warnings for async function in useEffect: useEffect function must return a cleanup function or nothing](https://stackoverflow.com/questions/53332321/react-hook-warnings-for-async-function-in-useeffect-useeffect-function-must-ret) – Drew Reese Sep 03 '20 at 05:21
  • Well, handleSearch is passed as props – Dariusz Legizynski Sep 03 '20 at 05:28
  • 1
    So you've not included all the relevant code for your issue then? The effect hook is the only code consuming `handleSearch` in your shared snippet. What if you declare `API_KEY` *outside* the component so it also isn't redefined each render cycle (*or if it needs to be internal to the component use `useRef` to hold on to it through render cycles*)? – Drew Reese Sep 03 '20 at 05:47
  • Sorry, was not intentional. Man, you are awesome! If I declare API_KEY outside, then the warning vanishes. – Dariusz Legizynski Sep 03 '20 at 06:08
  • 1
    Yup, no worries, I hadn't really noticed it until I was poking around in your sandbox (unsuccessfully reproducing the lint warning) and saw it hanging out there at the top. Cheers. – Drew Reese Sep 03 '20 at 08:04

0 Answers0