Can't reset Reactive with value is array

38 Views Asked by At
  • I got a problem, when i create an array initial Reactive()

  • And I want it reset whenever Type change. reset function

  • But not working.

  • Anyone has solution, please let me know, and can u please explain about why Object.assign can't work with array in Reactive(). Thanks!

1

There are 1 best solutions below

0
Jenuel Ganawed On

If you just want to reset your reactive data you can just do it like this. I have written different approach.

  1. This if your only using the index 0, you can just reset the index zero like this.
watch(() => record.value.type, () => {

  state[0] = {value: '', weight: ''};
})
  1. If you want to reset every item inside you state:
watch(() => record.value.type, () => {
  state.forEach((item, index) => {
    state[index] = {value: '', weight: ''};
  })
})
  1. If you want to reset everything, and start from the very start:
watch(() => record.value.type, () => {
  state.length = 0; // empty the array
  state[0] = {value: '', weight: ''} // add initial value
})