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

106
Visualizações
setState() Syntax seems wrong

Why does it work when I do "state:" and not "tasks:" in the addItem() function. I thought that the syntax is that you put the state that you want to change, which in this will be tasks:

import React from 'react';
import './App.css';

class App extends React.Component {
  constructor(){
    super();
    this.state = {
      value: '',
      tasks: []
    };

    this.handleChange = this.handleChange.bind(this)
    this.addItem = this.addItem.bind(this)
  }

  handleChange(event) {
      this.setState({
        value: event.target.value
      })
  }

  addItem(){
      this.setState({
      state: this.state.tasks.push(this.state.value),
      value: ''
      })

  }

  render(){

    const itemList = this.state.tasks.map((num,index) => <li key={index}>{num}</li>)
    return(
      <div>
              <input type="text" value={this.state.value} onChange={this.handleChange}></input>
              <button onClick={this.addItem}>+</button>
              <button>-</button>
              <ul>{itemList}</ul>  
      </div>
    )
  }
}


export default App;

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First of all you are mutating your state, this is the first rule when dealing with react state management. Never Mutate state. and you need to wrapper your event handlers in an anonymous function so you can pass you event object. This is your code fixed below---

import React from 'react';
import './App.css';

class App extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            value: '',
            tasks: []
         };

        this.handleChange = this.handleChange.bind(this)
        this.addItem = this.addItem.bind(this)
    }

    handleChange(event) {
        this.setState({
            value: event.target.value
        })
    }

    addItem(){
        const tempTasksArray = this.state.tasks.concat(this.state.value)
        this.setState({
           tasks: tempTasksArray,
         })
    }

   render(){
       const itemList = this.state.tasks.map((num,index) => <li key={index}>{num}</li>)
       return(
            <div>
                 <input 
                       type="text" 
                       value={ this.state.value } 
                       onChange={(event) => this.handleChange(event) } />
    
                 <button onClick={() => this.addItem }>
                     +
                </button>
                <button>
                    -
                </button>
             <ul>{ itemList }</ul>
          </div>
       )}
  }


   export default App;
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