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

121
Vistas
Can't insert state values into an object

I have this state in my class:

this.state = {
  inputValue: "",
  todos: []
}

In an arrow function, I am trying to setState in order to put inputValue into todos.

this.setState({ inputValue: "", todos: [...this.state.todos, {this.state.todos.length: this.state.inputValue}] })

However, when I try to insert the inputValue and the number, it gives an error. Any help is appreciated as I am new to React.

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

0

If you want a resulting todos array like this:

[
  {0: "something"},
  {1: "Else"}
]

The square bracket notation is needed to use a variable as the object key.

this.setState({
    inputValue: "",
    todos: [
      ...this.state.todos,
      { [this.state.todos.length]: this.state.inputValue }
    ]
});
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