7

Using Svelte Each Blocks: Const Directives & Destructuring | Rodney Lab

 3 years ago
source link: https://rodneylab.com/using-svelte-each-blocks/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
NEXT POST >
LATEST POST >>

Using Svelte Each Blocks: Const Directives & Destructuring #

Published: 2 days ago
3 minute read Gunning Fog Index: 6.6
Content by Rodney
SHARE:

?‍?‍?‍? Using Svelte #each Blocks #

This post on using Svelte each blocks will be helpful not only if you are new to Svelte and you are seeing each blocks for the first time. The video is also worth a watch if you are already comfortable with them. That is because as well as running through how each blocks work in Svelte, we will sprinkle in a few new Svelte features. Even experts might not yet have seen these in the wild!

You use Svelte each blocks to cycle through arrays and array-like objects. For example, in our code we have an array of RGB colours and display them to the browser, one-by-one. On top we make use of destructuring and Svelte const directives within the each block to simplify the code. If you are new to Svelte, it would be well worth cloning the app and adding extra features to cement your understanding. Anyway take a look at the video and do let me know how you use the code. If you are a Svelte pro also let me know how you optimise the code even further.

Also look out for the guest appearance by Svelte style directives!

? Using Svelte Each Blocks: Video #

Using Svelte Each Blocks: Const Directives & Destructuring

? Poll #

What will you use to create you next Svelte site?
  • Astro,
  • ElderJS,
  • Slinkity,
  • SvelteKit,
  • something else... drop a comment below to say what
Voting reveals latest results.

? Using Svelte Each Blocks: Code #

Final version of #each block #

src/routes/index.svelte
svelte
<script>
let colours = [
{ red: 0, green: 5, blue: 1 },
{ red: 247, green: 244, blue: 243 },
{ red: 255, green: 159, blue: 28 },
{ red: 48, green: 131, blue: 220 },
{ red: 186, green: 27, blue: 29 },
];
let colourCount = colours.length;

let colourSystem = 'hex';

// ...TRUNCATED see full version in GitHub repo (link below)
</script>

{#each colours as { red, green, blue }, index}
{@const hex = rgbToHex({ red, green, blue })}
{@const hsl = rgbToHsl({ red, green, blue })}
{@const colourClass = textColourClass({ red, green, blue })}
<article
style:background-color={hex}
aria-posinset={index + 1}
aria-setsize={colourCount}
class={`colour ${colourClass}`}
>
{#await colourName(hex)}
...
{:then name}
{`${name}: `}
{/await}
<span class="colour-code">
{#if colourSystem === 'hex'}
{hex}
{:else if colourSystem === 'rgb'}
rgb({red} {green} {blue})
{:else}
{hsl}
{/if}</span
>
</article>
{/each}

? Using Svelte Each Blocks: Links #

? Using Svelte Each Blocks: Summary #

How do you loop or iterate over an array in Svelte? #

Each blocks are helpful for looping, or iterating, over arrays and array-like objects in Svelte. They are a Svelte language feature so not unique to SvelteKit. On top the syntax is quite simple. The block's first line is wrapped in braces and starts #each: '{#each arrayName as elementName}'. You can then use elementName to access the current element of the input array within the block. Close the block with {/each}. If you need access to the index in the loop, add an extra variable to hold this. We have seen you can do this neatly, separating the two identifiers with a comma.

Can you declare variables in a Svelte each block? #

For convenience Svelte has an @const directive for declaring local variables within an each block. This can simplify code, making it easier to read. You might use this to store an intermediate or calculated value, especially if that value is used several times inside the each block.

Can you destructure array elements in a Svelte each block? #

We have seen destructuring works in Svelte each blocks and is another way to simplify your code. As well as destructuring, you can rename array element object fields. This can be handy if you want to add shortcut object syntax within your each block; change incoming object field names to match those in existing objects for cleaner code.

?? Feedback #

If you have found this video useful, see links below for further related content on this site. I do hope you learned one new thing from the video. Let me know if there are any ways I can improve on it. I hope you will use the code or starter in your own projects. Be sure to share your work on Twitter, giving me a mention so I can see what you did. Finally be sure to let me know ideas for other short videos you would like to see. Read on to find ways to get in touch, further below. If you have found this post useful, even though you can only afford even a tiny contribution, please consider supporting me through Buy me a Coffee.

Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on Twitter and also askRodney on Telegram . Also, see further ways to get in touch with Rodney Lab. I post regularly on SvelteKit as well as Search Engine Optimisation among other topics. Also subscribe to the newsletter to keep up-to-date with our latest projects.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK