-import { ref, unref, watch } from 'vue'
+import { ref, toRaw, unref, watch } from 'vue'
import { acceptHMRUpdate, defineStore } from '../../../src'
import { getRandomJoke, Joke } from '../views/api/jokes'
import useSWRV from 'swrv'
// const current = ref<null | Joke>(null)
const history = ref<Joke[]>([])
- const { data, error, isValidating, mutate, revalidate } = useSWRV(
- 'jokes',
- getRandomJoke
- )
+ const { data, error, isValidating, mutate } = useSWRV('jokes', getRandomJoke)
watch(data, (joke) => {
+ console.log('changed from within the store', joke)
if (joke) {
- history.value.push(joke)
+ history.value.push(toRaw(joke))
}
})
- return { current: data, history, fetchJoke: revalidate }
+ return { current: data, error, history, fetchJoke: mutate }
})
if (import.meta.hot) {
</template>
<script lang="ts" setup>
-import { computed, onMounted, ref } from 'vue'
+import { computed, onMounted, ref, watch } from 'vue'
import { useJokesSetup } from '../stores/jokes-swrv'
// const jokes = useJokes()
})
}
+watch(
+ () => jokes.current,
+ (joke) => {
+ console.log('Got joke', joke)
+ }
+)
+
onMounted(() => {
console.log('mounted')
// @ts-expect-error