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

oncreate issues with Nested Components in a loop of an array with items added before the end of the array #700

Closed
btakita opened this issue Jul 8, 2017 · 2 comments · Fixed by #704
Labels

Comments

@btakita
Copy link
Contributor

btakita commented Jul 8, 2017

It seems like there are some tracking issues with a loop, changed arrays (unshifted item), & nested component (manifested in oncreate).

https://svelte.technology/repl?version=1.24.0&gist=3d12f2bbd599b5d73443c0ded72c808c

The expected output is:

title__oncreate=Title 0

title__oncreate=Title 1

title__oncreate=Title 2
@btakita btakita changed the title oncreate issues with Nested Components in a loop of a changed array oncreate issues with Nested Components in a loop of a changed array (prepended item) Jul 8, 2017
@btakita btakita changed the title oncreate issues with Nested Components in a loop of a changed array (prepended item) oncreate issues with Nested Components in a loop of a changed array (unshifted item) Jul 8, 2017
@btakita
Copy link
Contributor Author

btakita commented Jul 8, 2017

Looks like using the same array & unshifting the item also has the same issue.

https://svelte.technology/repl?version=1.24.0&gist=3d12f2bbd599b5d73443c0ded72c808c

Also, there are issue with adding items in the middle of the array.

https://svelte.technology/repl?version=1.24.0&gist=a1a246237f812a6dfb6d27bc33392c93

Note that pushing the next item works...

https://svelte.technology/repl?version=1.24.0&gist=136c0713ac5f2220a5a5adac0e4b9fb2

@btakita btakita changed the title oncreate issues with Nested Components in a loop of a changed array (unshifted item) oncreate issues with Nested Components in a loop of an array with items added before the end of the array Jul 8, 2017
@Rich-Harris
Copy link
Member

This is expected behaviour — when you go from [b, c] to [a, b, c] you're not telling the first component 'you are now in the middle', you're telling it that it was b but it's now a. But if you're setting data in oncreate (and not using this.observe to keep it up to date) then it will stay fixed on the old, incorrect value.

In theory there's a simple way to get this to work, which is to use the syntax for keyed each blocks:

{{#each things as thing @id}}
  <!-- thing.id uniquely identifies each thing -->
{{/each}}

That way, you are telling the first component that it's now second, and the second component that it's now third.

Two problems however:

  • There's no way to say 'use the thing itself, rather than a property of the thing', which is what you need in this case (because each title is a string not an object)
  • There's a bug! For some reason, Title 0 is appearing at the end of the list when we use keyed updates.

@Rich-Harris Rich-Harris added the bug label Jul 8, 2017
Rich-Harris added a commit that referenced this issue Jul 10, 2017
use anchor comments to preserve order in keyed each blocks with components
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants