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

195
Vistas
React toDoApp removing a task functionality not working

I am a new to React and trying to make a toDoApp. I'm trying to create an onClick event to remove an element from the tasks state. But it does not seem to work. And can I also ask how to implement id keys so that I can remove the desired task that I want?

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)
    this.removeItem = this.removeItem(this)
  }

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

  addItem(){
    if(this.state.value === ''){
      alert('Enter a task!')
    } else {
      const newTask = [...this.state.tasks, this.state.value]
      this.setState({
      tasks: newTask,
      value: ''
      })
    }
    }

  removeItem(){
    this.state.tasks.pop()
    this.setState({
      tasks: this.state.tasks,
    })

  }

  render(){

    const itemList = this.state.tasks.map((num) => 
              <li>{num}</li>
            );

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


      </div>
    )
  }
}


export default App;

strong text

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

0

pop() removes the last element, but React will not rerender the component as setState is called with the same array. You can create a new array and use it for updating the state.

removeItem() {
    this.state.tasks.pop()
    this.setState({
      tasks: [...this.state.tasks],
    })
}
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