Sphere
Log in Join
☆ 5

Vue 3 props lose reactivity when I destructure them

Vue 3.4, script setup. I am destructuring props so I can use them in a computed, but the value never updates when the parent changes it. const { userId } = defineProps<{ userId: number }>() and then const name = computed(() => store.users[userId].name). When userId changes in the parent the computed stays on the old id. I am told to use toRefs but toRefs on defineProps feels wrong. What is the correct pattern here in 3.4?

⚑ Report

4 replies

☆ 0
S shipit ·2026-10-18#1

You need const props = defineProps(...) then const { userId } = toRefs(props). userId becomes a ref so the computed tracks it. Or just do not destructure: use props.userId in the computed. Vue 3.5 has reactive props destructure as a stable feature but in 3.4 you need toRefs.

⚑ Report
☆ 0
K kp ·2026-10-25#2

toRefs worked, thanks. One thing, my computed had userId.value in it after the change and I forgot the .value so it compared a ref to a number and was always false. Fixed that too. Two small mistakes stacked on top of each other.

⚑ Report
☆ 0
P pwned ·2026-10-27#3

Yeah that .value gets everyone. If you are on 3.4 and hate toRefs, you can also do const userId = computed(() => props.userId). Then you get a ref-like thing without destructuring at all. Slightly more verbose but clear. We actually hit this in two components before someone spotted the pattern.

⚑ Report
☆ 0
L loot ·2026-10-28#4

Small add: if you upgrade to 3.5 the destructure is reactive by default, so your original code would just work. We did the upgrade last month and deleted a bunch of toRefs calls. Watch out for the defineModel changes if you use that though.

⚑ Report
Log in to reply.