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

234
Visualizações
TypeError: Invalid attempt to spread non-iterable instance. In set an Array into state in class component

I'm using React Navigation to get an array called additional using this.props.route.params when I navigate to a page, and when I render the screen I want to set the state details in the class component with the array additional, I'm still new to JavaScript and this is what I came up with:

Array

    Array [
      Object {
        "data": Array [
          Object {
            "id": 1.01,
            "price": 1,
            "selected": false,
            "title": "Ranch",
            "type": "Sides",
          },
          Object {
            "id": 1.02,
            "price": 1,
            "selected": false,
            "title": "Blue Cheese",
            "type": "Sides",
          },
        ],
        "required": false,
        "type": "Sides",
      },
      Object {
        "data": Array [
          Object {
            "id": 2.01,
            "price": 1,
            "selected": false,
            "title": "Hot Sauce",
            "type": "Sauces",
          },
          Object {
            "id": 2.02,
            "price": 1,
            "selected": false,
            "title": "Medium Sauce",
            "type": "Sauces",
          },
        ],
        "required": true,
        "type": "Sauces",
      },
    ]

Screen

class DisplayItem extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sides: [],
      details: [],
    }
  }

  render() {
    const { additional } = this.props.route.params;
    const { sides, details } = this.state;

    if (additional !== undefined) {
      sides.forEach((item) => {
        item['selected'] = false
      });
      additional.forEach((a, i) => {
        a["data"] = sides.filter(b => b.type == a.type);
      });
      this.setState((details) => {return [...details, additional]})
    }

I've attempted to use ()=>this.setState({details: additional}) but it returns only an empty array, then I've tried the above method and it returns the error: TypeError: Invalid attempt to spread non-iterable instance.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You are trying to spread an array ...details, which is not possible.

Can you put debugger or console.log before this.setState((details) => {return [...details, additional]}) and can you tell us what is in additional?

Btw in ()=>this.setState({details: additional}) you are trying to set additional which you probably assume is array, but it probably is just a string value.

about 4 years ago · Juan Pablo Isaza Relatório

0

You are trying to spread the whole state which is an object not the array part of it

Here you can destruct the details part from the current state before spreading it like this:

this.setState(({details}) => {return [...details, additional]})
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