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

137
Views
Update all components whe use onClick event in React JS

I have conditional rendering. When i use onClick event all my components update. My functions.

const reaction = (author) => {
    if (author == user.id){
        return false
    }else{
        return true
    }
}

const handleReactions = (reaction, id) =>{
    if (reaction === 'like'){
        httpClient.put(`/likes/${id}/`)
            .then((response) => {
                alert(response.data);
            })
    }
}

My return

return (
    {reaction(post.user)
     ?<form>
      <button type="submit" name="like" value={post.id} onClick={handleReactions('like', post.id)}>
    : //another conditional
    }
)
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Try something like :

    return (
    {reaction(post.user)
     ?<form>
      <button type="submit" name="like" value={post.id} onClick={() => handleReactions('like', post.id)}>
    : //another conditional
    })

Explanation: you are calling handleReactions directly instead of giving the function to execute when the button is clicked. Solution define lambda function which is calling your function inside

PS : your function :

const reaction = (author) => {
    if (author == user.id){
        return false
    }else{
        return true
    }
}

Can be simplified:

const reaction = (author) => author != user.id
about 4 years ago · Santiago Gelvez 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!