Skip to content

Are nested elements allowed in pinia #1099

Discussion options

You must be logged in to vote

It seems due to your items[1].colors[0] and items[2].colors[0] share the same object.

Maybe you forgot to deeply clone it?

For example, assuming you have an addItem function like this:

const initialColors = [{ active: true, hex: "000000", id: 0 }]
function addItem () {
  const newItem = {
    colors: [...initialColors], // forgot to deeply clone here
    colors: JSON.parse(JSON.stringify(initialColors)), // ok

    // ...
  }
  state.items.push(newItem)
}

Further reading: Immerjs is a library to help us solve this kind of problem in a elegant way.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@david8z
Comment options

@sbay-ppm
Comment options

Answer selected by david8z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants