Skip to content

Instantly share code, notes, and snippets.

@Kcko
Created February 7, 2025 14:42
Show Gist options
  • Save Kcko/da6f13bde273c9b772f39b28e690605b to your computer and use it in GitHub Desktop.
Save Kcko/da6f13bde273c9b772f39b28e690605b to your computer and use it in GitHub Desktop.
<script setup>
import { shallowRef, watchEffect, triggerRef } from 'vue'
const user = shallowRef({
name: 'John',
job: 'BFU',
age: 90
})
// Log the user whenever it changes
watchEffect(() => {
console.log('LOG: watchEffect', user)
});
// Update nested state (no log happens)
setTimeout(() => {
user.value.name = 'Martin';
console.log('LOG: user name changed')
}, 2000)
// Force a reactive update to trigger
setTimeout(() => {
triggerRef(user);
console.log('LOG: whole object changed')
}, 4000)
// [user object]
</script>
<
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment