Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/composition-api-vs-react-hooks-differences.md
1---2title: Vue Composition API Runs Once, Unlike React Hooks3impact: MEDIUM4impactDescription: Understanding this difference prevents over-engineering and React patterns that don't apply5type: gotcha6tags: [vue3, composition-api, react-hooks, setup, stale-closure]7---89# Vue Composition API Runs Once, Unlike React Hooks1011**Impact: MEDIUM** - Vue's `setup()` or `<script setup>` executes only once per component instance, while React Hooks run on every render. Developers coming from React often apply patterns (dependency arrays, excessive memoization, useCallback) that are unnecessary and counterproductive in Vue.1213Understanding this fundamental difference is crucial for writing idiomatic Vue code. Vue's approach eliminates entire categories of bugs (stale closures, exhaustive deps) that plague React applications.1415## Task Checklist1617- [ ] Don't implement "dependency arrays" - Vue tracks dependencies automatically18- [ ] Don't wrap functions in "useCallback" equivalents - not needed in Vue19- [ ] Don't use "useMemo" patterns - Vue's `computed()` handles this automatically20- [ ] Understand that closures in Vue don't go "stale" like in React21- [ ] Don't worry about "call order" - Vue composables can be conditional2223**React Patterns to Avoid in Vue:**24```javascript25// These patterns are UNNECESSARY in Vue - they solve React-specific problems2627// WRONG: Trying to implement dependency arrays (React pattern)28watch(29[dep1, dep2, dep3], // Vue tracks deps automatically in watchEffect30() => {31// ...32}33)34// Unless you specifically WANT to control which deps trigger the watcher,35// prefer watchEffect() which auto-tracks3637// WRONG: Memoizing callbacks like useCallback38const memoizedHandler = computed(() => {39return () => doSomething(state.value)40})41// In Vue, just define the function normally - no memoization needed4243// WRONG: Worrying about stale closures44function useData() {45const data = ref(null)4647// In React, this could capture stale 'data' - NOT in Vue!48// Vue refs are always current49const handler = () => {50console.log(data.value) // Always gets current value51}5253return { data, handler }54}55```5657**Correct Vue Patterns:**58```javascript59import { ref, computed, watchEffect } from 'vue'6061// CORRECT: Auto-dependency tracking with watchEffect62const query = ref('')63const filter = ref('all')6465watchEffect(() => {66// Vue automatically detects that this depends on query and filter67// No dependency array needed!68fetchResults(query.value, filter.value)69})7071// CORRECT: computed() handles memoization automatically72const expensiveResult = computed(() => {73// Only recalculates when dependencies actually change74return heavyComputation(data.value)75})7677// CORRECT: Functions don't need memoization78function handleClick() {79count.value++80}81// Just use it directly - no useCallback wrapper needed82// <button @click="handleClick">8384// CORRECT: Closures always access current values85const count = ref(0)86const message = ref('')8788function logState() {89// This always logs CURRENT values, never stale ones90console.log(`Count: ${count.value}, Message: ${message.value}`)91}9293setTimeout(() => {94logState() // Gets current values even if called later95}, 5000)96```9798## Vue's Advantages Over React Hooks99100```javascript101// 1. No stale closure problems102const count = ref(0)103104onMounted(() => {105setInterval(() => {106// In React: would need useRef or deps array to avoid stale value107// In Vue: count.value is always current108console.log(count.value)109}, 1000)110})111112// 2. Composables can be conditional113if (featureEnabled) {114const { data } = useSomeFeature() // This is FINE in Vue!115}116// In React: "Hooks cannot be conditional" - not a problem in Vue117118// 3. No exhaustive-deps linting headaches119watchEffect(() => {120// Use any reactive values - Vue tracks them all automatically121// No ESLint rule yelling about missing dependencies122doSomething(a.value, b.value, c.value)123})124125// 4. Child components don't need memoization by default126// Vue's reactivity system only updates what actually changed127// No need for React.memo() equivalents in most cases128```129130## When Vue Patterns Differ131132```javascript133// Setup runs once - so initialization happens once134<script setup>135import { ref, onMounted } from 'vue'136137// This code runs ONCE when component is created138const data = ref(null)139console.log('Setup running') // Only logs once140141onMounted(() => {142console.log('Mounted') // Only logs once143})144145// If you need something to run on every reactive change,146// use watch or watchEffect147watchEffect(() => {148// This runs when dependencies change149console.log('Data changed:', data.value)150})151</script>152```153154## Reference155- [Composition API FAQ - Relationship with React Hooks](https://vuejs.org/guide/extras/composition-api-faq.html#relationship-with-react-hooks)156- [Reactivity Fundamentals](https://vuejs.org/guide/essentials/reactivity-fundamentals.html)157