In Vue,shallowReactive
andshallowRef
Are two for creationshallow responsive dataAPI. They are withreactive
andref
Similar, but behaves differently when dealing with nested objects. Below are detailed interpretations and examples of them.
1. shallowReactive
effect
shallowReactive
Create a shallow responsive object. Only the top-level properties of the object are responsive, and the properties of nested objects are not converted to responsive.
Use scenarios
- When you only need the top-level properties of an object to be responsive and don't care about the responsiveness of nested objects.
- When responsive transformations of nested objects can bring performance overhead.
Example
import { shallowReactive, effect } from 'vue'; const state = shallowReactive({ foo: 1, nested: { bar: 2, }, }); effect(() => { ('foo changed:', ); // Responsive}); effect(() => { (' changed:', ); // Non-responsive}); = 10; // Trigger the first effect = 20; // The second one will not be triggered effect
explain:
-
It is responsive, modifying it will trigger dependency updates.
-
It is not responsive, modifying it will not trigger dependency updates.
2. shallowRef
effect
shallowRef
Create a shallow layerResponsive citations,only.value
The attribute itself is responsive, and.value
Internal properties will not be converted to responsive.
Use scenarios
- When you just need
.value
It is responsive, not caring.value
When the internal attributes are responsive. - when
.value
is a complex object and does not require deep listening.
Example
import { shallowRef, effect } from 'vue'; const count = shallowRef({ value: 1, }); effect(() => { ('count changed:', ); // Non-responsive}); = 10; // Effect will not be triggered = { value: 20 }; // trigger effect
explain:
-
It is responsive, modifying it will trigger dependency updates.
-
Not responsive, but directly modified
Dependency update will be triggered.
3. shallowReactive
andshallowRef
The difference
characteristic | shallowReactive |
shallowRef |
---|---|---|
Object of action | Object | Any value (usually used for objects or complex data) |
Responsive range | Only the top-level properties are responsive | only .value It's responsive |
Nested object processing | The properties of nested objects are not responsive |
.value The internal properties are not responsive |
Typical usage scenarios | Only top-level attribute responsive objects are required | Just need .value Responsive references |
4. shallowReactive
andreactive
Comparison
reactive
Deep responsiveness
import { reactive, effect } from 'vue'; const state = reactive({ foo: 1, nested: { bar: 2, }, }); effect(() => { (' changed:', ); // Responsive}); = 20; // trigger effect
reactive
The entire object and its nested properties will be converted into responsive.
shallowReactive
shallow responsiveness
import { shallowReactive, effect } from 'vue'; const state = shallowReactive({ foo: 1, nested: { bar: 2, }, }); effect(() => { (' changed:', ); // Non-responsive}); = 20; // Will not trigger effect
shallowReactive
Only convert the top-level attributes to responsiveness, and the nested attributes remain unchanged.
5. shallowRef
andref
Comparison
ref
Deep responsiveness
import { ref, effect } from 'vue'; const count = ref({ value: 1, }); effect(() => { (' changed:', ); // Responsive}); = 10; // trigger effect
ref
Will be.value
Its internal properties are converted into responsive forms.
shallowRef
shallow responsiveness
import { shallowRef, effect } from 'vue'; const count = shallowRef({ value: 1, }); effect(() => { (' changed:', ); // Non-responsive}); = 10; // Effect will not be triggered = { value: 20 }; // trigger effect
shallowRef
Only.value
It is converted into responsive, and the internal properties remain unchanged.
6. Summary of usage scenarios
shallowReactive
- Suitable for objects that only require top-level attribute responsiveness.
- For example: the top-level field of form data.
shallowRef
- Suitable for only needed
.value
Responsive reference. - For example: a reference to a DOM element or an object that does not require deep listening.
7. Things to note
Performance optimization:
shallowReactive
andshallowRef
It can reduce unnecessary responsive conversions, thereby improving performance.
Responsiveness of nested objects:
If you need responsiveness of nested objects, you should usereactive
orref
。
.value
Use of:
shallowRef
of.value
It is responsive, but.value
The internal properties are not responsive.
8. Summary
-
shallowReactive
andshallowRef
It is a shallow responsive API provided by Vue 3. -
shallowReactive
Only convert the top-level properties of the object into responsiveness. -
shallowRef
Only.value
Convert itself to responsive. - They are suitable for scenarios where performance is required or that are not required for deep responsiveness.
Through reasonable useshallowReactive
andshallowRef
, can optimize the performance of Vue applications while ensuring functions.
This is the article about the detailed interpretation of shallowReactive and shallowRef. For more detailed interpretation of shallowReactive and shallowRef, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!