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>)}
π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
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
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
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()`