mehulmpt / firebase-react-hooks

Firebase + React Hooks app demo - https://www.youtube.com/watch?v=K_wZCW6wXIo
106 stars 76 forks source link

React Hook #1

Open KamusiimeRaymond opened 5 years ago

KamusiimeRaymond commented 5 years ago

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 commented 5 years ago

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

matiaspunx commented 4 years ago

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 commented 4 years ago

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
}