mangomango
mangomango

Reputation: 23

Why doesn't the state value changed by useEffect come back?

const [showModal, setShowModal] = useState(false);

    useEffect(()=>{
        if (!isMobile)
            if (collectShow)
                if (closeModal)
                    if (controllModal) {
                        setShowModal (true)
                    }else{
                        setShowModal (false)
                    }
    },[isMobile, collectShow, closeModal, controllModal])

    console.log (showModal)

I want to keep changing the value of showModal like this, but once changed, it stays the same even if the conditions change. I'm new to react, so I think I used the wrong useEffect, please help

Upvotes: 1

Views: 40

Answers (1)

DownloadPizza
DownloadPizza

Reputation: 3456

I think you dont want nested ifs. Right now your code basically means

if (!isMobile && collectShow && closeModal)
  setShowmodal(controllModal) 

So it only changes when the first condition is true

While you probably want

setShowmodal(!isMobile && collectShow && closeModal && controllModal) 

At least thats my assumption. If its not what you want I can only recommend abusing console.log to see where you are going

Upvotes: 2

Related Questions