Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): set ref in the post-render queue #1835

Merged
merged 4 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions packages/runtime-core/__tests__/apiTemplateRef.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,61 @@ describe('api: template refs', () => {
// ref should be updated
expect(serializeInner(root)).toBe(`<div id="foo">foo</div>`)
})

// #1834
test('exchange refs', async () => {
const refToggle = ref(false)
const spy = jest.fn()

const Comp = {
render(this: any) {
return [
h('p', { ref: refToggle.value ? 'foo' : 'bar' }),
h('i', { ref: refToggle.value ? 'bar' : 'foo' })
]
},
mounted(this: any) {
spy(this.$refs.foo.tag, this.$refs.bar.tag)
},
updated(this: any) {
spy(this.$refs.foo.tag, this.$refs.bar.tag)
}
}

const root = nodeOps.createElement('div')
render(h(Comp), root)

expect(spy.mock.calls[0][0]).toBe('i')
expect(spy.mock.calls[0][1]).toBe('p')
refToggle.value = true
await nextTick()
expect(spy.mock.calls[1][0]).toBe('p')
expect(spy.mock.calls[1][1]).toBe('i')
})

// #1789
test('toggle the same ref to different elements', async () => {
const refToggle = ref(false)
const spy = jest.fn()

const Comp = {
render(this: any) {
return refToggle.value ? h('p', { ref: 'foo' }) : h('i', { ref: 'foo' })
},
mounted(this: any) {
spy(this.$refs.foo.tag)
},
updated(this: any) {
spy(this.$refs.foo.tag)
}
}

const root = nodeOps.createElement('div')
render(h(Comp), root)

expect(spy.mock.calls[0][0]).toBe('i')
refToggle.value = true
await nextTick()
expect(spy.mock.calls[1][0]).toBe('p')
})
})
24 changes: 19 additions & 5 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,28 @@ export const setRef = (
}

if (isString(ref)) {
refs[ref] = value
if (hasOwn(setupState, ref)) {
queuePostRenderEffect(() => {
const doSet = () => {
refs[ref] = value
if (hasOwn(setupState, ref)) {
setupState[ref] = value
}, parentSuspense)
}
}
// #1789: for non-null values, set them after render
// null values means this is unmount and it should not overwrite another
// ref with the same key
if (value) {
queuePostRenderEffect(doSet, parentSuspense)
} else {
doSet()
}
} else if (isRef(ref)) {
ref.value = value
if (value) {
queuePostRenderEffect(() => {
ref.value = value
}, parentSuspense)
} else {
ref.value = value
}
} else if (isFunction(ref)) {
callWithErrorHandling(ref, parentComponent, ErrorCodes.FUNCTION_REF, [
value,
Expand Down