Subscribe on changes!

A new toRefs, which supports converting ref.value of object type to ref.

avatar
Mar 14th 2022

What problem does this feature solve?

In Vue3, because the object generated by reactive can not be reassigned, we generally use the method of const objRef = ref(object) to define an object that needs to be assigned repeatedly. However, such a definition will make it cumbersome to read the attributes of the object, for example:

interface ObjType {
  name: string
  age: number
  sex?: string
}
const objRef = ref<ObjType>({ name: 'H', age: 1 })
objRef.value.name // 'H'
objRef.value.age // 1

I hope to provide a quick reading method through toRefs, for example:

const { name, age } = toRefs(objRef)
const sex = toRef(objRef, 'sex', 'boy')

What does the proposed API look like?

I have realized this function, but I have added a new api, which does not replace the original toRefs, so I hope to discuss with you whether it exists and if so, what does it look like? If appropriate, I will mention a PR. I put the corresponding code in my repository.

https://github.com/hcg1023/vue3-dnd/blob/main/lib/internals/toReactive.ts

avatar
Mar 14th 2022

I think the cumbersome-ness would go away once we have a stable Reactivity Transform.

If you still want to add a new API for your usecase, please start a discussion in the RFC repository instead.

avatar
Mar 14th 2022

I think the cumbersome-ness would go away once we have a stable Reactivity Transform.

If you still want to add a new API for your usecase, please start a discussion in the RFC repository instead.

OK, I'll go to RFC repository to mention it