vanjs-org / van

🍦 VanJS: World's smallest reactive UI framework. Incredibly Powerful, Insanely Small - Everyone can build a useful UI app in an hour.
https://vanjs.org
MIT License
3.77k stars 87 forks source link

array function push, splice doesn't trigger state change and redraw #358

Open kangaroolab opened 2 weeks ago

kangaroolab commented 2 weeks ago
let subnodes = van.state([]);
...
button({
                innerHTML: '+',
                onclick: () =>
                    {subnodes.push(key + '.' + (subnodes.length + 1))}
            }),

if I do above, it doesn't trigger any change

if I change onclick to

onclick: () =>
                    {subnodes.val = [...subnodes.val, (key + '.' + (subnodes.val.length + 1))]}

it works as expected.

same issue/behavior with subnodes.val.splice

any insight why push/splice doesn't work?

Tao-VanJS commented 2 weeks ago

This is intended behavior. VanJS uses reference equality check to determine whether a state has changed. If you call push or splice method, the reference to the underlying array won't change, thus won't trigger a DOM update. If you want the DOM tree to be updated on the mutation of an array, you can take a look at vanX.list in VanX extension.