We've seen how we can use {#each} to repeat content in @sveltejs ,

let's talk about the keyed each block, and what does it mean to have a key for each block



πŸ‘‡πŸ§΅[THREAD]
πŸ“ΉIf you prefer learning through video, the following thread is a summary from my @YouTube video

πŸ’‘ You can always subscribe to my channel to get notified for the next awesome content!

πŸ‘‰πŸ‘‰ youtube.com/lihautan?sub_c…
Let's first talk about how {#each} block updates

When you add a new item
πŸ‘‰{#each} doesn't care where the item is added
πŸ‘‰{#each} iterate through every item
πŸ‘‰{#each} checks every item and updates them one by one

[1/9]
When you remove an item
πŸ‘‰{#each} doesn't care where the item is removed
πŸ‘‰{#each} iterate through every item
πŸ‘‰{#each} checks every item and updates them one by one
πŸ‘‰{#each} removes any extraneous items

[2/9]
You'll notice that

πŸ’‘if you insert/remove an item in the middle of the list, every subsequent item in the list gets updated
πŸ’‘{#each} block has no idea which element is added / removed
πŸ’‘a more optimised approach would be to only insert / remove the changed item

[3/9]
That's where the keyed each block comes in! πŸŽ‰

To add a key to the each block, add a parentheses with the expression of the key

πŸ’‘the key can be any expression

{#each <array> as <item in array> (<key of item>)}

[4/9] {#each array as person (per...
Before the {#each} block updates, it will

πŸ‘‰use the key to create a mapping of key <-> item
πŸ‘‰use the mapping to lookup for item when updating the list

[5/9]
πŸ‘‰ {#each} loop through the target result to figure any items not added in the mapping (item previously in the list), create and add it to the mapping
πŸ‘‰ {#each} iterate through the current list, if the key doesn't match, inserts the item from the mapping

[6/9]
πŸ’‘1 item is added, no item is updated

[7/9]
πŸ‘‰ {#each} iterate through the current list, if the key doesn't match, remove from the current list
πŸ‘‰ at the end of the iteration, any unused item is removed

πŸ’‘1 item is removed, no item is updated

[8/9]
πŸ‘‰ {#each} iterate through the current list, if the key doesn't match, remove from the current list
πŸ‘‰ {#each} if the key doesn't match, but exist in the mapping, insert the item from mapping

πŸ’‘1 item is removed, no item is updated

[9/9]
πŸ“Ή If you want a more detailed step-through, watch my doing it over @YouTube

[END] Follow @lihautan for more content like this, we are going to look at await block next!

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Tan Li Hau πŸ€”

Tan Li Hau πŸ€” Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @lihautan

6 Jul
If you have a promise waiting for your data, {#await} block makes it easy to express your content in the loading πŸ•‘, resolved βœ… and rejected ❌ state

So, let's talk about the {#await} block in @sveltejs

πŸ‘‡πŸ§΅[THREAD]
Here's the general structure for the {#await} block

πŸ•‘ {#await}
βœ… {:then}
❌ {:catch}
πŸ‘‹ {/await}

[1/4] <script>   let promise; </s...
All the states are optional, so you could have

😜 no loading state
πŸ˜† no resolved state
πŸ˜… no rejected state

[2/4] <script>   let promise; </s...<script>   let promise; </s...<script>   let promise; </s...
Read 8 tweets
1 Jul
HTML doesn't have a way of expressing logic, like conditionals and loops. @sveltejs does.

Let's take a look at the 1st logic block in @sveltejs, πŸ‘‰ {#if} πŸ‘ˆ

πŸ‘‡πŸ§΅ [THREAD]
If you prefer watching video, the following thread is a summary from my @YouTube video
πŸ’‘{#if} is our 1st logic logic block in @sveltejs
πŸ’‘it allow us to conditional render content
πŸ’‘if comes with else, else if, just like you would expect in JavaScript <script>   let value = 42; </script>  {#if value === 42}   <
Read 8 tweets
29 Jun
How do you pass dynamic data when you forward slots in @sveltejs?
Let's recap, here is how you forward slots
and here is how you pass dynamic data into slots

Read 11 tweets
5 Apr
How much do you know about the 7 event modifiers in @sveltejs?

Let's take a look what you need to write if you don't have them πŸ‘‡ Image
1️⃣ preventDefault

this is equivalent of calling event.preventDefault() on the start of the event handler Image
2️⃣ stopPropagation

this is equivalent of calling event.stopPropagation() on the start of the event handler Image
Read 9 tweets
15 Aug 20
Another week on #CSSpodcast, this weeks topic is CSS Functions 😍

🧡
CSS Functions provides runtime contextual expressions that return dynamic real-time value per the state of the browser per user in that moment
- global space, no need import
- can be nested, calc(var(--var))
- typed, eg: `rotate(45px)` will not work
- keep the function live, recompute on value changes and updates
- many of them are pure functions, however, counter-example: `counter()`
Read 20 tweets
12 Jul 20
In the latest episode #CSSpodcast, @Una and @argyleink shared a ton of pseudo-classes

(I managed to note down 3️⃣9️⃣ of them 😱)

🧡 Here is a thread of what were shared in the podcast πŸ‘‡πŸ‘‡πŸ‘‡
Pseudo-classes are selectors, lets you apply style to element based on info outside of the doc tree:

🌎 meta information, eg: language, history of navigator
β˜‘οΈ state of the element, eg: checked, active, disabled

✨Pseudo-classes are great for implementing micro-interactivity
πŸ”€ Case-insensitive
⏹ No whitespace in between
Read 24 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(