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

154
Views
Button onClick not firing in Reactjs

Buttons are not doing what they are supposed to do, adding to good, neutral, bad feedback.

Curiously, I added a default button that uses different syntax to add to bad, and it works. So there is something wrong with my "giveGoodFeedback" functions.

import React, { useState } from 'react'


const Button = (props) => {
  return (
    <button onClick={props.handleClick}>
      {props.text}
    </button>)
}

const App = () => {
  // save clicks of each button to its own state
  const [good, setGood] = useState(0)
  const [neutral, setNeutral] = useState(0)
  const [bad, setBad] = useState(0)

  const giveGoodFeedback = () => {
    setGood( good + 1 )
  }

  const giveNeutralFeedback = () => {
    setNeutral( neutral + 1 )
  }

  const giveBadFeedback = () => {
    setBad( bad + 1 )
    console.log('Bad increases')
  }

  return (
    <div>
      <h1>give feedback</h1>
      <Button text='good' onClick={giveGoodFeedback}/>
      <Button text='bad' onClick={giveBadFeedback}/>
      <Button text='neutral' onClick={giveNeutralFeedback}/>
      <button onClick={() => setBad(bad + 1)}>
        Click me
      </button>
      <h1>Statistics</h1>
      <ul>
        <li>good : {good}</li>
        <li>neutral : {neutral}</li>
        <li>bad : {bad}</li>
      </ul>
      code here
    </div>
  )
}

export default App;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Because you are using incorrect props, it should be props.onClick , please have a try. Thanks me later👍

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!