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

103
Views
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 answers
Answer question

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 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!