Conditions:
{#if user.loggedIn}
<button on:click={toggle}>
Log out
</button>
{/if}
{#if !user.loggedIn}
<button on:click={toggle}>
Log in
</button>
{/if}
Conditional Render:
{#if condition}
<p>Condition is true</p>
<p>OtherCondition is true</p>
<p>Any Condition is true</p>
{/if}
// Re render
{#key value}
<div transition:fade>{value}</div>
{/key}
Loops:
{#each cats as cat, i}
<li><a target="_blank" href="https://www.youtube.com/watch?v={cat.id}">
{i}: {cat.name}
</a></li>
{/each}
Tip: You can also de-structure as:
{#each cats as { id, name }}and then use{name}instead of{cat.name}
When a value isn’t exported but is computed by a prop, you can go into problems when updating values since it’s a copy and not a reference. Thus, be sure to identify the right object when updating its value(s):
{#each cats as cat, i (cat)} // Notice the (cat)
<li><a target="_blank" href="https://www.youtube.com/watch?v={cat.id}">
{i}: {cat.name}
</a></li>
{/each}
Promises:
{#await promise}
<p>Loading...</p>
{:then result}
<p>It returns {result}</p>
{:catch error}
<p>An error occurred: {error.message}</p>
{/await}
Tip: You can also skip loading step by using:
{#await promise then result}