preactjs / signals

Manage state with style in every framework
https://preactjs.com/blog/introducing-signals/
MIT License
3.63k stars 88 forks source link

Array splice doesn't trigger re-render #510

Closed chinmaypant21 closed 4 months ago

chinmaypant21 commented 4 months ago

There is no re-render trigger while updating an array stored in a signal using array splice. Here is an example of the issue:

const tasks = signal<any[]>(['Task1', 'Task2', 'Task3']);

const Screen = () => {
    function handleRemoveTask(idx: number){
    activeWindows.value.splice(idx,1)
    ))
  }
  return (
  <>...</>
  )
}
XantreDev commented 4 months ago

Signals can only rerender if you assign new value with .value property. Splice is a mutating array in place. So you need to use: tasks.value = tasks.value.toSpliced(...)

rschristian commented 4 months ago

Yup. working as intended.