Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
why Can i not spread an array of object with Zustand?

I am using Zustand in a Nextjs app using Typescript, For some reason I get a runtime error message whenever I try to iterate on my state object.

The structure of my car damaged zone object is:

const damagedZones = {
  Left: {
    front_door:[
      {
      id: 1
      picture1: 'picture 1 base64 string',
      picture2: 'picture 2 base64 string',
      comment: 'any comment'
      },
      {
      id: 2
      picture1: 'picture 1 base64 string',
      picture2: 'picture 2 base64 string',
      comment: 'any comment'
      }
    ],
    back_door: [
    ],

so, imagine I add a new object to my "front_door" array, here is my zustand store and function: In the code below, the dynamic prop "zone" would be my "Left" key of my damagedZones object, and the dynamic prop "element" would be my "front_door" key.

export const useDamagedZones = create<DamagedZonesProps>((set) => ({
  damagedZones: damagedZones,
  setDamagedZones: (elementItem: damagedItem, zone: string, element: string) => {
    set(state => ({
      damagedZones: {
        ...state.damagedZones,
        [zone]: {
          ...state.damagedZones[zone],
          [element]: [
            ...state.damagedZones[zone]?.[element],
            elementItem
          ]
        }
      }
    }))
  },
}))

so basically when I trigger this function, I get a runtime error which says:

TypeError: Invalid attempt to spread non-iterable instance. In order to be iterable, non-array objects must have a Symbol.iterator method.

I am not understanding why that is so.... I have tried using an object instead of an array, with an id as key, and it works fine, but it's not super convenient, so an array is the best in this situation, but it doesn't perform as expected....

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Alright i figured it out, it is because i misconceived my typescript types for my damagedZone object ! i forgot to mention that one key would be an array :)

it works now that my object type is like so :

type damagedItem = {
  id?: number 
  picture1?: string | null,
  picture2?: string | null,
  picture3?: string | null,
  comment?: string,
}

type DamagedElement = {
  [key: string]: damagedItem[]
}

type DamagedZone = {
  step1_gauche: DamagedElement,
  step1_droite: DamagedElement,
  step1_avant: DamagedElement,
  step1_arriere: DamagedElement
}

and here is the store useDamagedZones for now :

export const useDamagedZones = create<DamagedZonesProps>((set) => ({
  damagedZones: damagedZones,
  setDamagedZones: (elementItem: damagedItem, zone: string, element: string) => {
    set(state => ({
      damagedZones: {
        ...state.damagedZones,
        [zone]: {
          ...state.damagedZones[zone],
          [element]: [
            ...state.damagedZones[zone]?.[element],
            elementItem
          ]
        }
      }
    }))
  },
  removeDamagedItem : (zone: string, element: string, id: number ) => {
    set(state => ({
      damagedZones: {
        ...state.damagedZones,
        [zone]: {
          ...state.damagedZones[zone],
          [element]: 
            state.damagedZones[zone]?.[element].filter(el => el.id !== id)
        }
      }
    }))
  }
}))
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!