Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kangaroolab opened this issue Aug 28, 2024 · 1 comment
Open

Comments

@kangaroolab
Copy link

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
Copy link
Member

Tao-VanJS commented Aug 28, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants