Merge pull request #3416 from sveltejs/better-keyed-each-example

better keyed each example
pull/3417/head
Rich Harris 5 years ago committed by GitHub
commit 120ee28c4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,11 +2,11 @@
import Thing from './Thing.svelte'; import Thing from './Thing.svelte';
let things = [ let things = [
{ id: 1, value: 'a' }, { id: 1, color: '#0d0887' },
{ id: 2, value: 'b' }, { id: 2, color: '#6a00a8' },
{ id: 3, value: 'c' }, { id: 3, color: '#b12a90' },
{ id: 4, value: 'd' }, { id: 4, color: '#e16462' },
{ id: 5, value: 'e' } { id: 5, color: '#fca636' }
]; ];
function handleClick() { function handleClick() {
@ -22,14 +22,14 @@
<div> <div>
<h2>Keyed</h2> <h2>Keyed</h2>
{#each things as thing (thing.id)} {#each things as thing (thing.id)}
<Thing value={thing.value}/> <Thing current={thing.color}/>
{/each} {/each}
</div> </div>
<div> <div>
<h2>Unkeyed</h2> <h2>Unkeyed</h2>
{#each things as thing} {#each things as thing}
<Thing value={thing.value}/> <Thing current={thing.color}/>
{/each} {/each}
</div> </div>
</div> </div>

@ -1,9 +1,24 @@
<script> <script>
// `value` is updated whenever the prop value changes... // `current` is updated whenever the prop value changes...
export let value; export let current;
// ...but `valueAtStart` is fixed upon initialisation // ...but `initial` is fixed upon initialisation
const valueAtStart = value; const initial = current;
</script> </script>
<p>{valueAtStart} / {value}</p> <p>
<span style="background-color: {initial}">initial</span>
<span style="background-color: {current}">current</span>
</p>
<style>
span {
display: inline-block;
padding: 0.2em 0.5em;
margin: 0 0.2em 0.2em 0;
width: 4em;
text-align: center;
border-radius: 0.2em;
color: white;
}
</style>

@ -2,11 +2,11 @@
import Thing from './Thing.svelte'; import Thing from './Thing.svelte';
let things = [ let things = [
{ id: 1, value: 'a' }, { id: 1, color: '#0d0887' },
{ id: 2, value: 'b' }, { id: 2, color: '#6a00a8' },
{ id: 3, value: 'c' }, { id: 3, color: '#b12a90' },
{ id: 4, value: 'd' }, { id: 4, color: '#e16462' },
{ id: 5, value: 'e' } { id: 5, color: '#fca636' }
]; ];
function handleClick() { function handleClick() {
@ -19,5 +19,5 @@
</button> </button>
{#each things as thing} {#each things as thing}
<Thing value={thing.value}/> <Thing current={thing.color}/>
{/each} {/each}

@ -1,9 +1,24 @@
<script> <script>
// `value` is updated whenever the prop value changes... // `current` is updated whenever the prop value changes...
export let value; export let current;
// ...but `valueAtStart` is fixed upon initialisation // ...but `initial` is fixed upon initialisation
const valueAtStart = value; const initial = current;
</script> </script>
<p>{valueAtStart} / {value}</p> <p>
<span style="background-color: {initial}">initial</span>
<span style="background-color: {current}">current</span>
</p>
<style>
span {
display: inline-block;
padding: 0.2em 0.5em;
margin: 0 0.2em 0.2em 0;
width: 4em;
text-align: center;
border-radius: 0.2em;
color: white;
}
</style>

@ -2,11 +2,11 @@
import Thing from './Thing.svelte'; import Thing from './Thing.svelte';
let things = [ let things = [
{ id: 1, value: 'a' }, { id: 1, color: '#0d0887' },
{ id: 2, value: 'b' }, { id: 2, color: '#6a00a8' },
{ id: 3, value: 'c' }, { id: 3, color: '#b12a90' },
{ id: 4, value: 'd' }, { id: 4, color: '#e16462' },
{ id: 5, value: 'e' } { id: 5, color: '#fca636' }
]; ];
function handleClick() { function handleClick() {
@ -19,5 +19,5 @@
</button> </button>
{#each things as thing (thing.id)} {#each things as thing (thing.id)}
<Thing value={thing.value}/> <Thing current={thing.color}/>
{/each} {/each}

@ -1,9 +1,24 @@
<script> <script>
// `value` is updated whenever the prop value changes... // `current` is updated whenever the prop value changes...
export let value; export let current;
// ...but `valueAtStart` is fixed upon initialisation // ...but `initial` is fixed upon initialisation
const valueAtStart = value; const initial = current;
</script> </script>
<p>{valueAtStart} / {value}</p> <p>
<span style="background-color: {initial}">initial</span>
<span style="background-color: {current}">current</span>
</p>
<style>
span {
display: inline-block;
padding: 0.2em 0.5em;
margin: 0 0.2em 0.2em 0;
width: 4em;
text-align: center;
border-radius: 0.2em;
color: white;
}
</style>
Loading…
Cancel
Save