Skip to content

Commit

Permalink
fix hydration error, ClientOnly component
Browse files Browse the repository at this point in the history
  • Loading branch information
ulfgebhardt committed Nov 23, 2023
1 parent 1937776 commit e97b813
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/components/ClientOnly.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<template>
<template v-if="isMounted"><slot /></template>
<template v-else><slot name="placeholder" /></template>
</template>

<script setup>
import { ref, onMounted } from 'vue'
const isMounted = ref(false)
onMounted(() => {
isMounted.value = true
})
</script>
14 changes: 11 additions & 3 deletions src/pages/app/index.page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,30 @@
<div v-if="page === undefined">
<h1>The Counter</h1>
<p>
The current value of the counter is: <b>{{ counter.count }}</b>
The current value of the counter is:
<ClientOnly
><b>{{ counter.count }}</b></ClientOnly
>
</p>
</div>
<div v-else-if="page === 'inc'">
<h1>Increase the Counter</h1>
<v-btn elevation="2" @click="counter.increment()">{{ counter.count }}</v-btn>
<ClientOnly>
<v-btn elevation="2" @click="counter.increment()">{{ counter.count }}</v-btn>
</ClientOnly>
</div>
<div v-else-if="page === 'reset'">
<h1>Reset the Counter</h1>
<v-btn elevation="2" @click="counter.reset()">{{ counter.count }}</v-btn>
<ClientOnly>
<v-btn elevation="2" @click="counter.reset()">{{ counter.count }}</v-btn>
</ClientOnly>
</div>
</template>
</DefaultLayout>
</template>

<script lang="ts" setup>
import ClientOnly from '#components/ClientOnly.vue'
import DefaultLayout from '#layouts/DefaultLayout.vue'
import { useCounterStore } from '#stores/counter'
Expand Down

0 comments on commit e97b813

Please sign in to comment.