Best practice for composable input argument "MaybeRefOrGetter" does not always guarantee reactivity if computed value is used in the composable #13270
ryanki-te
started this conversation in
General Discussions
Replies: 1 comment 3 replies
-
I don’t think there’s actually a difference here. In your example you pass a getter to computed, so Vue can track its dependencies and keep the result reactive. Composable parameters, by contrast, aren’t always meant to update over time. Using |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We've recently encountered an reactivity issue with the computed property inside the composable and the specific argument that we pass in to the composable.
From the vue's best practice article related to the input argument states that we should use
MaybeRefOrGetter
. Then, you can call the composables as such:But if you define the composable that requires a computed value based on the argument:
and call
useFeature(1)
,num2
doesn't compute ifnum
changes.Well, you could think that it's because you passed in the static value, there's no way to change or make it reactive... but often in our use of the composables, we pass in prop values from the component, like
useFeature(prop.foo)
.What's the issue with this? Well, if we use the prop to get another computed value, i.e.
const v2 = computed(() => props.v1);
vue automatically figures out that this is reactive and computes the value whenever the prop changes. But if we pass this into our composable, the computed value doesn't change as the prop changes.It is this difference that I think we shouldn't suggest we pass
MaybeRefOrGetter
as arguments to composables. The definition ofMaybeRefOrGetter
type allows a raw value to be passed in. And I believe this can lead to bugs and unexpected behavior, as we have find the hard way... Should passing in of a raw value be recommended for composables?It was the best practice guide in vue's documentation that we've used
MaybeRefOrGetter
type, but having to find this exception, I feel pretty strong about this is not the best practice for composable.@yyx990803 Love to hear your opinion as well, if you have time. Thanks!
Here's a sample program that demonstrates this issue.
Beta Was this translation helpful? Give feedback.
All reactions