Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

361
Visualizações
what method should i write in onChange so that the value will display in webpage using reactjs

I have started learning react but stuck here. i want to print the value in webpage but cant get what method should i put in onChange and please need a explanation also.

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 Respostas
Responde à pergunta

0

change this line

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

to

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

this.change is a handler for the onChange event which take the value and updates the state.

about 4 years ago · Juan Pablo Isaza Relatório

0

Try setting onChange={this.change}. You have this method defined but aren’t using it, instead passing setState as if it were an event handler, which it isn't.

about 4 years ago · Juan Pablo Isaza Relatório

0

Make the following changes:

  • Bind the methods correctly in the constructor.

  • Set onChange to this.change and not this.setState.

  • For updating a piece of state use this.setState and don't mutate the state.

  • And in the change method, coerce the value of the input to a Number for increment and decrement operations to work properly.

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda