Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
React JS open a modal on button press if a user is not logged in

I'm trying to make a voting system like stackoverflow, when a user clicks a button to vote up / down, if he's not logged in, a pop-up modal is displayed. My user information is stored in context so I can use a hook to check if I have currently sign in user. The question is how can I properly make a check inside onClick handler.

My first suggestion was something like this.

function userVote(){

  const [showModal, setShowModal] = useState();

  const auth = useAuth();

  const handleClick = () => !auth.user ? setShowModal(true) : "the logic for updating the user 
  vote if he is already signed in"

  return (
    <>
      <button onClick={handleClick}></button>
      {showModal ? <Modal /> : null}
    </>
  )
}

But as far as I know, it is not a good practice to make state updates inside conditions or I'm wrong. I suppose there is a better way of doing this.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

if you want to get rid of the condition before updating the state modify your function to be like:

const handleClick = () => {
  setShowModal(!auth.user)
  if (auth.user) {
  // Write the logics here
  }
}

it sets the state no matter if auth.user exists or not.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!