support isShallow
What problem does this feature solve?
The reactivity package lacks a way to determine the shallow type, so add an isShallow
method like what isReadonly
do.
I want to use isShallow
in my project to judge if a reactive is of type shallow.
What does the proposed API look like?
import { isShallow, shallowReactive } from 'vue'
const foo = shallowReactive({ test: 1 })
if (isShallow(foo)) {
// ... do something
}
I still think that this requires a real use case, rather than a simple code demonstration. I don’t see what scenarios users need to care about whether the data is shallow or not.
I still think that this requires a real use case, rather than a simple code demonstration. I don’t see what scenarios users need to care about whether the data is shallow or not.
其实我觉得isReadonly和isShallow类似
My use case is a clone function where I want to accept any reactive object and return the a new object of the exact same type.
const source = reactive({ hello: 'world' })
const copy = clone(source)
source.hello = 'bob'
copy.hello // 'world'
But there's an issue if you pass in something that is shallow.
const source = shallowReactive({ hello: ref('world') })
const copy = clone(source)
source.hello // RefImp
copy.hello // 'world'
This happens because I can check if the source is reactive, but I cannot check if it is shallow. So it ends up returning reactive
instead of shallowReactive
.
function clone(source) {
let copy
// code that actually does the data copying
if(isReactive(source) {
return reactive(copy)
}
}
But what I'd like to be able to do is
function clone(source) {
let copy
// code that actually does the data copying
if(isReactive(source) {
return isShallow(source) ? shallowReactive(copy) : reactive(copy)
}
}
isShallow()
was added in https://github.com/vuejs/core/commit/9fda9411ec92dc703288b229106116dd0f16e5e7. It has been available since 3.2.28.