Skip to content

Commit 6505ade

Browse files
docs: add transition + key example (#2705)
1 parent d3e37ab commit 6505ade

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/guide/built-ins/transition.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,31 @@ This can be useful when you've defined CSS transitions / animations using Vue's
581581

582582
You can also apply different behavior in JavaScript transition hooks based on the current state of your component. Finally, the ultimate way of creating dynamic transitions is through [reusable transition components](#reusable-transitions) that accept props to change the nature of the transition(s) to be used. It may sound cheesy, but the only limit really is your imagination.
583583

584+
## Transitions with the Key Attribute {#transitions-with-the-key-attribute}
585+
586+
Sometimes you need to force the re-render of a DOM element in order for a transition to occur.
587+
588+
Take this counter component for example.
589+
590+
```vue
591+
<script setup>
592+
import { ref } from 'vue';
593+
const count = ref(0);
594+
595+
setInterval(() => count.value++, 1000);
596+
</script>
597+
598+
<template>
599+
<Transition>
600+
<span :key="count">{{ count }}</span>
601+
</Transition>
602+
</template>
603+
```
604+
605+
If we had excluded the `key` attribute, only the text node would be updated and thus no transition would occur. However, with the `key` attribute in place, Vue knows to create a new `span` element whenever `count` changes and thus the `Transition` component has 2 different elements to transition between.
606+
607+
[Try it in the Playground](https://play.vuejs.org/#eNp9UsFu2zAM/RVCl6Zo4nhYd/GcAtvQQ3fYhq1HXTSFydTKkiDJbjLD/z5KMrKgLXoTHx/5+CiO7JNz1dAja1gbpFcuQsDYuxtuVOesjzCCxx1MsPO2gwuiXnzkhhtpTYggbW8ibBJlUV/mBJXfmYh+EHqxuITNDYzcQGFWBPZ4dUXEaQnv6jrXtOuiTJoUROycFhEpAmi3agCpRQgbzp68cA49ZyV174UJKiprckxIcMJA84hHImc9oo7jPOQ0kQ4RSvH6WXW7JiV6teszfQpDPGqEIK3DLSGpQbazsyaugvqLDVx77JIhbqp5wsxwtrRvPFI7NWDhEGtYYVrQSsgELzOiUQw4I2Vh8TRgA9YJqeIR6upDABQh9TpTAPE7WN3HlxLp084Foi3N54YN1KWEVpOMkkO2ZJHsmp3aVw/BGjqMXJE22jml0X93STRw1pReKSe0tk9fMxZ9nzwVXP5B+fgK/hAOCePsh8dAt4KcnXJR+D3S16X07a9veKD3KdnZba+J/UbyJ+Zl0IyF9rk3Wxr7jJenvcvnrcz+PtweItKuZ1Np0MScMp8zOvkvb1j/P+776jrX0UbZ9A+fYSTP)
608+
584609
---
585610

586611
**Related**

0 commit comments

Comments
 (0)