Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
Why does it not push the element into the array state "tasks"?

I'm trying to create a todo app in React. I'm currently trying to print the input when i press the + button, but it is not working. Can anyone help to check my syntax!

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.state.tasks.push(this.state.value)
  }

  render(){

    return(
      <div>
              <input type="text" value={this.state.value} onChange={this.handleChange}></input>
              <button onClick={this.addItem}>+</button>
              <button>-</button>
              <h1>{this.state.tasks}</h1>  
      </div>
    )
  }
}


export default App;

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

To change the state you should use this.setState. Refer to https://reactjs.org/docs/state-and-lifecycle.html#do-not-modify-state-directly. Reading React documentation might save you a lot of time overall.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to setState task

addItem() {
    const newTask = [this.state.value, ...this.state.tasks];
    this.setState({ tasks: newTask });
    this.setState({ value: "" });
  }

enter image description here

about 4 years ago · Juan Pablo Isaza Denunciar

0

Modifying state directly is not the correct way!

instead use setState:

this.setState({
  tasks: this.state.tasks.concat(this.state.value)
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda