7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Svelte 3.28.0で追加された{#key}を使ってみた

Posted at

{#key}

Svelte 3.28.0で{#key}が追加されました。

svelte3.28.0

{#key}は指定した値が変わったときにブロック内の要素も更新してくれるみたいです。

いままでは{#each}を使って実現してたようです。

<script>
	import { fade, slide } from 'svelte/transition';
	let ary = ['cat', 'dog', 'fox', 'wolf', 'panda'];
	let count = 0;
	setInterval(() => {
		count ++;
    if (count >= ary.length) {
        count = 0;
    }
	}, 1500);	
</script>

{#each [count] as count (count)}
	<span in:fade="{{delay: 150}}" out:slide>{ary[count]}</span>
{/each}

<style>
	span {
		position: absolute;
	}
</style>

Svelte 3.28.0


<script>
	import { fade, slide } from 'svelte/transition';
	let ary = ['cat', 'dog', 'fox', 'wolf', 'panda'];
	let count = 0;
	setInterval(() => {
		count ++;
    if (count >= ary.length) {
        count = 0;
    }
	}, 1500);	
</script>

{#key count}
	<span in:fade="{{delay: 150}}" out:slide>{ary[count]}</span>
{/key}

<style>
	span {
		position: absolute;
	}
</style>

実際の動き

key

参考資料

Twitter:Svelte Society

7
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?