Ref vs. React in Vue 3?
P粉743288436
2023-08-27 20:35:30
<p>Check out some examples from some people's Vue 3 preview tutorials. [Currently in beta]</p>
<p>I found two examples:</p>
<h2>Reaction formula</h2>
<pre class="brush:js;toolbar:false;"><template>
<button @click="increment">
Count is: {{ state.count }}, double is: {{ state.double }}
</button>
</template>
<script>
import { reactive, computed } from 'vue'
export default {
setup() {
const state = reactive({
count: 0,
double: computed(() => state.count * 2)
})
function increment() {
state.count
}
return {
state,
increment
}
}
}
</script>
</pre>
<h2>Reference</h2>
<pre class="brush:js;toolbar:false;"><template>
<div>
<h2 ref="titleRef">{{ formattedMoney }}</h2>
<input v-model="delta" type="number">
<button @click="add">Add</button>
</div>
</template>
<script>
import { ref, computed, onMounted } from "vue";
export default {
setup(props) {
//State
const money = ref(1);
const delta = ref(1);
// Refs
const titleRef = ref(null);
// Computed props
const formattedMoney = computed(() => money.value.toFixed(2));
//Hooks
onMounted(() => {
console.log("titleRef", titleRef.value);
});
// Methods
const add = () => (money.value = Number(delta.value));
return {
delta,
money,
titleRef,
formattedMoney,
add
};
}
};
</script>
</pre>
<p><br /></p>
There are some similarities between
ref
andreactive
in that they both provide a way to store data and allow the data to be reactive.but:
High Level Difference:
Source: Vue Forum Discussion
Reaction formula
reactive
Gets the object and returns a reactiveproxy
to the original object.Example
illustrate
In the above, whenever we want to change or access the properties of
page
,For example
page.ads
,page.filteredAds
will be updated through the proxy.Points
reactive()
Only accepts objects, not JS primitives (String, Boolean, Number, BigInt, Symbol, null, undefined)ref()
Calling behind the scenesreactive()
reactive()
works on objects, andref()
callsreactive()
, objects work on bothref()
has a.value
attribute for reassignment,reactive()
does not have this attribute, and therefore cannot be reassigneduse
ref()
When..'string'
,true
,23
, etc.)reactive()
When..ref()
Summarize
ref()
seems to be the way to go since it supports all object types and allows reassignment using.value
.ref()
is a good starting point, but as you get used to the API, you'll know thatreactive()
has less overhead, and you may find it more capable Meet your needs.ref()
Use caseFor primitives you will always use
ref()
, butref()
is useful for objects that need to be reallocated (such as arrays).The above
reactive()
needs to reassign a property rather than the entire object.reactive()
Use caseA good use case for reactive() is a set of primitives that belong together:
The above code feels more logical than the above code
Useful links
If you're still lost, this simple guide helped me: https://www.danvega.dev/blog/2020/02/12/vue3-ref-vs-reactive/
Use only arguments from
The decisions behindref()
: https://dev.to/ycmjason/thought-on-vue-3-composition-api-reactive-considered-harmful-j8creactive()
andref()
and other important information exist in the Vue Composition API RFC: https://vuejs.org/guide/extras /composition-api-faq. html#why-composition-api