Skip to content

Commit 048b6d3

Browse files
pikaxantfu
andauthoredJun 24, 2020
fix(toRefs): do not warn when toRefs is called in a prop value (#405)
* fix(toRefs): do not warn when toRefs is called in a prop value * chore: comments * Update test/setup.spec.js Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com> Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent d70c904 commit 048b6d3

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed
 

‎src/setup.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,12 @@ export function mixin(Vue: VueConstructor) {
220220
if (isFunction(bindingValue)) {
221221
bindingValue = bindingValue.bind(vm)
222222
}
223+
// unwrap all ref properties
224+
const unwrapped = unwrapRefProxy(bindingValue)
225+
// mark the object as reactive
226+
markReactive(unwrapped)
223227
// a non-reactive should not don't get reactivity
224-
bindingValue = ref(markRaw(unwrapRefProxy(bindingValue)))
228+
bindingValue = ref(markRaw(unwrapped))
225229
}
226230
}
227231
asVmProperty(vm, name, bindingValue)

‎test/setup.spec.js

+49
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,55 @@ describe('setup', () => {
176176
)
177177
})
178178

179+
it('not warn doing toRef on props', async () => {
180+
const Foo = {
181+
props: {
182+
obj: {
183+
type: Object,
184+
required: true,
185+
},
186+
},
187+
setup(props) {
188+
return () =>
189+
h('div', null, [
190+
h('span', toRefs(props.obj).bar.value),
191+
h('span', toRefs(props.obj.nested).baz.value),
192+
])
193+
},
194+
}
195+
196+
let bar
197+
let baz
198+
199+
const vm = new Vue({
200+
template: `<div id="app"><Foo :obj="obj" /></div>`,
201+
components: { Foo },
202+
setup() {
203+
bar = ref(3)
204+
baz = ref(1)
205+
return {
206+
obj: {
207+
bar,
208+
nested: {
209+
baz,
210+
},
211+
},
212+
}
213+
},
214+
})
215+
vm.$mount()
216+
217+
expect(warn).not.toHaveBeenCalled()
218+
expect(vm.$el.textContent).toBe('31')
219+
220+
bar.value = 4
221+
baz.value = 2
222+
223+
await vm.$nextTick()
224+
expect(warn).not.toHaveBeenCalled()
225+
expect(vm.$el.textContent).toBe('42')
226+
})
227+
179228
it('should merge result properly', () => {
180229
const injectKey = Symbol('foo')
181230
const A = Vue.extend({

0 commit comments

Comments
 (0)
Please sign in to comment.