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

364
Views
qué método debo escribir en onChange para que el valor se muestre en la página web usando reactjs

Empecé a aprender a reaccionar, pero me quedé estancado aquí. Quiero imprimir el valor en la página web, pero no puedo obtener qué método debo poner en Cambio y también necesito una explicación.

 class IncrementDecrement extends React.Component { constructor(props) { super(props); this.state = { a: 0 }; this.change = this.onIncreased.bind(this); this.change = this.onDecreased.bind(this); } change(event) { this.setState({ a: event.target.value }); } onIncreased() { this.state.a = this.state.a + 1; console.log(this.state.a); } onDecreased() { this.state.a = this.state.a - 1; console.log(this.state.a); } render() { return ( <div> <input type="number" value={this.state.a} onChange={this.setState} /> <button onClick={() => { this.onIncreased(); }} > Increment </button> <button onClick={() => { this.onDecreased(); }} > Decrement </button> </div> ); } } ReactDOM.render(<IncrementDecrement />, document.getElementById("root"));
 <script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script> <div id="root"></div>

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

0

cambiar esta linea

 <input type="number" value={this.state.a} onChange={this.setState} />

a

 <input type="number" value={this.state.a} onChange={this.change} />

this.change es un controlador para el evento onChange que toma el valor y actualiza el estado.

about 4 years ago · Juan Pablo Isaza Report

0

Intente configurar onChange={this.change} . Tiene este método definido pero no lo está usando, sino que pasa setState como si fuera un controlador de eventos, que no lo es.

about 4 years ago · Juan Pablo Isaza Report

0

Realice los siguientes cambios:

  • Enlace los métodos correctamente en el constructor.

  • Establezca onChange en this.change y no this.setState .

  • Para actualizar una parte del estado, use this.setState y no cambie el estado.

  • Y en el método de change , coaccione el valor de la entrada a un Number para que las operaciones de incremento y decremento funcionen correctamente.

 class IncrementDecrement extends React.Component { constructor(props) { super(props); this.state = { a: 0 }; this.change = this.change.bind(this); this.onIncreased = this.onIncreased.bind(this); this.onDecreased = this.onDecreased.bind(this); } change(event) { this.setState({ a: Number(event.target.value) }); } onIncreased() { this.setState({ a: this.state.a + 1 }); } onDecreased() { this.setState({ a: this.state.a - 1 }); } render() { return ( <div> <input type="number" value={this.state.a} onChange={this.change} /> <button onClick={() => { this.onIncreased(); }} > Increment </button> <button onClick={() => { this.onDecreased(); }} > Decrement </button> </div> ); } } ReactDOM.render(<IncrementDecrement />, document.getElementById("root"));
 <script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script> <div id="root"></div>

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!