In svelte, I’m currently templating out an array of messages
like this:
{#each messages as message, i}
{@const isLast = i === messages.length - 1}
<div class="card" class:active={isLast}>
{message}
</div>
{/each}
I then have a function where I can append a new item to the array
function addNextWord(){
messages = [...messages, words[index]];
}
When I do so, I’d like to scroll the very last item into view (using something like Element.scrollIntoView
).
But how can I get a reference to just the last item in the loop?
2
Answers
As far as I can tell, you can't conditionally set a binding on an element, so the two strategies would be to set a
bind:this
on the parent container or on an array of child items and then pick what one you want from there.Using the
bind:this
directive allows you to reference a DOM element from a variable in svelte.Once you have a reference to the parent you can use
Element.lastElementChild
to get the last child from a parent element.Finally, you'll have to wait for a
tick
for the data changes to propagate to the DOM before selecting the last item.Put all together, it should look something like this:
Demo in Svelte REPL
See Also: How to scroll to the last item when adding a new one on a list?
Just use an action.
REPL
In the vast majority of cases actions are just better than
bind:this
.