Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Hook useEffect has a missing dependency #6903

Closed
Emre-Hacilar opened this issue Apr 25, 2019 · 2 comments
Closed

React Hook useEffect has a missing dependency #6903

Emre-Hacilar opened this issue Apr 25, 2019 · 2 comments

Comments

@Emre-Hacilar
Copy link

Is this a bug report?

No

Issue

I am using inside hooks the useEffect for componentDidMount. Additionally I have a useContext.
I am calling a function via the context for changing a state with a given param.
Although this works fine, I get a warning inside the console which looks like following:

React Hook useEffect has a missing dependency: 'langContext'. Either include it or remove the dependency array react-hooks/exhaustive-deps

I tried many things from other issues but nothing worked for me.

Code

import React, { useState, useEffect, useContext } from 'react';
import LanguageContext from 'utils/context';

const IsLoggedIn = (props) => {
    const langContext = useContext(LanguageContext);
    const [isLoading, setIsLoading] = useState(true);

    useEffect(() => {
        if (verifyToken()) {
                langContext.setLanguage('de')
                setIsLoading(false);
        } else {
            setIsLoading(false);
        };
    }, []);

    const content = (isLoading) ? <Spinner full /> : pageContent;

    return content;
}

Question

My question is, is this an actual error or a linting problem and how do I fix this warning?

Thanks in advance.

@ordazgustavo
Copy link

The linter is telling you that you're depending on external values that may change, when you set the dependencies as an empty array the effect will run just once when the component mounts.

You can read more about that here.

@ianschmitz
Copy link
Contributor

Also see #6880 for more discussion on this topic.

@lock lock bot locked and limited conversation to collaborators Apr 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants