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 #1

Open
KamusiimeRaymond opened this issue Sep 17, 2019 · 3 comments
Open

React Hook #1

KamusiimeRaymond opened this issue Sep 17, 2019 · 3 comments

Comments

@KamusiimeRaymond
Copy link

Line 46: React Hook "useState" is called conditionally. React Hooks must be called in the exact same order in every component render.

please explain.

@KamusiimeRaymond
Copy link
Author

Line 48: React Hook "useEffect" is called conditionally. React Hooks must be called in the exact same order in every component render

@matiaspunx
Copy link

Hey!
I think you need to move this func

if (!firebase.getCurrentUsername()) { // not logged in alert('Please login first') props.history.replace('/login') return null }

after useState and useEffect, bc that return null its the actual problem.

@fruit-ninja
Copy link

fruit-ninja commented Nov 15, 2020

The rules of hooks are that it cannot be in loops, conditions, or nested functions

https://stackoverflow.com/questions/57620799/react-hook-useeffect-is-called-conditionally

const {classes} = props

const [quote, setQuote] = useState('')

useEffect(() => {
    if(firebase.getCurrentUsername()) {
        firebase.getCurrentUserQuote().then(setQuote)
    }
}, [firebase.getCurrentUsername(), firebase.getCurrentUserQuote()])

if(!firebase.getCurrentUsername()) {
    // Not logged in
    alert('Please login first')
    props.history.replace('/login')
    return null
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants