skip to Main Content

I got this OnClick:

const [open, setOpen] = useState(true);

and

onClick={() => setOpen(!open != true)}

Aim is to "when open then close" and "when closed then remain closed". Above works for the second part but not the first. How to make both my requirements work?

2

Answers


  1. If it is only going to be closed when clicking, then you only need to pass false to setOpen()

    const [open, setOpen] = useState(true);
    onclick = e => {
      setOpen(false);
    };
    

    I might be getting something confused so please tell me if I am.

    Login or Signup to reply.
  2. Set the onClick method like this:

    onClick={() => setOpen(!open)}
    

    This will make sure that:

    1. open becomes false when it is true
    2. open becomes true when its is false
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search