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

161
Views
How do I listen for a number value in reactjs

I have made a shopping cart in react. I have assigned a value to each button. The value shows in the dom so I know it's there.

I created a function to capture the value and then push it into an array. However, when I hit add to cart it say's "nothing added". I've been trying to find a way to listen for the value in the event listener but nothing seems to work.

I have tried putting just e.target.value but the outcome is the same.

Any ideas?

const [item, setItem] = useState([]);

function addCart(e) {
    if (e.target.value === "") {
      item.push(e.target.value);
      console.log(item);
      setCart(cart + 1);
    } else {
      console.log("nothing added");
    }
  }

 <button
              value={pro.price}
              onClick={addCart}
              className="bg-blue-500 text-white font-bold border-white p-2 rounded-md"
            >
              {"add to cart"}
            </button>
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I think you need to negate the condition in the if block. e.target.value !== ""

about 4 years ago · Juan Pablo Isaza Report

0

If you want to update your state item, you've to use setItem.

function addCart(e) {
    if (e.target.value !== "") {
      setItem([...item, e.target.value]);
      console.log(item);
      setCart(cart + 1);
    } else {
      console.log("nothing added");
    }
  }
about 4 years ago · Juan Pablo Isaza Report

0

I suggest that you could change your approach and use a class instead of a function as such :

class AddCard extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      value: 'VALUE'
    }
  }
  handleChange (e) {
    console.log('handleChange called')
  }
  handleClick () {
    this.setState({value: 'UPDATED_VALUE'})
    var event = new Event('input', { bubbles: true });
    this.myinput.dispatchEvent(event);
  }
  render () {
    return (
      <div>
        <input readOnly value={this.state.value} onChange={(e) => {this.handleChange(e)}} ref={(input)=> this.myinput = input}/>
        <button onClick={this.handleClick.bind(this)}>Change Input</button>
      </div>
    )
  }
}

ReactDOM.render(<AddCard />,  document.getElementById('app'))

By doing so you'll be able to dissociate the click event and the change event

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!