Replies: 2 comments 4 replies
-
Why do you put these refs inside If you really like this style, maybe consider using regular <template>
<h1>{{ title }}</h1>
</template>
<script>
export default {
setup() {
const state = reactive({
title: "Hello",
});
state.title += " World!";
return state;
}
};
</script> |
Beta Was this translation helpful? Give feedback.
4 replies
-
Have you used any composables yet? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello there,
I recently heard that the Reactivity Transform RFC has been dropped, as it involved more ambiguity than it solved. I have an ongoing (experimental) project which used it, and even though I was glad not having to read or write
.value
anymore, I was not 100% satisfied either, since you never really know whether a JS variable is, or isn't, a ref (yeah I don't use TypeScript and I should, but that's not the point).Anyway, I had to refactor my existing code to remove every use of the reactivity transform macro, in favor of something else, trying to avoid
get / set / unref / .value / etc
as much as possible.The problem:
.value
is uglyOf course you can use get or set from
@vueuse/core
to make things prettier, but the point is that you are required to know that you're working on refs, and when your ref contains an object, you have to access it likeget(obj).property
which feels even uglier.The solution:
reactive
all the things!You will tell me, you can actually get rid of
.value
by encapsulating all your refs within areactive
object:Sounds good at first, as you access a property of a
state
or aself
object (name it whatever you want), which feels more natural asfirstName.value
, but then:state.fullName
which is fine in the<script>
part, but definitely horrible into the<template>
part.Just one source of truth, please
This way you can do anything you want inside
<script setup>
by reading or writingstate.*
properties, being assured thatstate
is reactive anyway.The only drawback that remains: inside
<template>
you have to prefix everything bystate.
sincefirstName
,lastName
andfullName
aren't top-level variables.Final Suggestion
How about some new attribute on
<script setup>
which would populate the template with the properties of a given reactive object, like<script setup expand="state">
which would allow some syntax like this?This would solve the
.value
problem, without adding too much "magic" or cognitive load.Other syntaxes that could look cool:
Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions