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

128
Visualizações
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 Respostas
Responde à pergunta

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