State
How to store information in components and refresh them?
For storing information in component use one of these functions:
ref
reactive
computed
readonly
For storing information that are reactive only root level properties use one of these functions:
shallowRef
shallowReactive
shallowReadonly
ref()
Takes an inner value and returns a reactive and mutable ref object, which has a single property .value that points to the inner value.
ref method is used to convert non-object variables into objects and then to grant reactive. ref is pretty much the reactive that works also with primitives.
The ref object is mutable.
- you can assign new values to .value - it is also reactive
- any read operations to .value are tracked, and write operations will trigger associated effects.
If an object is assigned as a ref's value, the object is made deeply reactive with reactive(). This also means if the object contains nested refs, they will be deeply unwrapped.
reactive()
Not finished
https://stackoverflow.com/questions/66585688/what-is-the-difference-between-ref-toref-and-torefs
The reactive conversion affects all nested properties. A reactive object also deeply unwraps any properties that are refs while maintaining reactivity.
If your original variable is already an object (or array), a ref wrapping is not needed because it is already a reference type. It only needs Vue's reactive functionality.
Note that refs are not unwrapped when accessed as array or collection elements:
When assigning a ref to a reactive property, that ref will also be automatically unwrapped:
edge cases to watch
-
the recursive creation of nested proxies can only happen if there is a nested object. If a given property does not exist, or it exists but it is not an object, no proxy can be created at that property. E.g. this is why reactivity does not work off the baz variable created by const baz = state.baz, nor the bar variable of const bar = state.foo.bar. To make it clear, what it means is that you can use state.baz and state.foo.bar in your template/computed/watch, but not baz or bar created above.
-
if you extract a nest proxy out to a variable, it is detached from its original parent. This can be made clearer with an example. The second assignment below
(state.foo = {bar: 3})
does not destroy the reactivity of foo, but state.foo will be a new proxy object while the foo variable still points the to original proxy object.
computed()
readonly()
Takes an object (reactive or plain) or a ref and returns a readonly proxy to the original.
Any nested property accessed will be readonly as well. It also has the same ref-unwrapping behavior as reactive(), except the unwrapped values will also be made readonly.
shallowRef()
Shallow version of ref().
shallowRef() is typically used for performance optimizations of large data structures, or integration with external state management systems.
shallowReactive()
Shallow version of reactive().
Unlike reactive(), there is no deep conversion: only root-level properties are reactive for a shallow reactive object. Property values are stored and exposed as-is - this also means properties with ref values will not be automatically unwrapped.
Shallow data structures should only be used for root level state in a component. Avoid nesting it inside a deep reactive objects.
shallowReadonly()
Shallow version of readonly().
Shallow data structures should only be used for root level state in a component.