Skip to content

Commit dff8acf

Browse files
committed
refactor(runtime-vapor): allow non-instance for setRef
1 parent 56a7f9d commit dff8acf

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

packages/runtime-vapor/__tests__/apiExpose.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('api: expose', () => {
2323
define({
2424
setup: () => {
2525
const n0 = (i = createComponent(Child))
26-
setRef(currentInstance as VaporComponentInstance, n0, childRef)
26+
setRef(n0, childRef)
2727
return n0
2828
},
2929
}).render()
@@ -46,7 +46,7 @@ describe('api: expose', () => {
4646
define({
4747
setup: () => {
4848
const n0 = createComponent(Child)
49-
setRef(currentInstance as VaporComponentInstance, n0, childRef)
49+
setRef(n0, childRef)
5050
return n0
5151
},
5252
}).render()

packages/runtime-vapor/src/apiTemplateRef.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,32 @@ export type setRefFn = (
3434

3535
export function createTemplateRefSetter(): setRefFn {
3636
const instance = currentInstance as VaporComponentInstance
37-
return (...args) => setRef(instance, ...args)
37+
return (el, ref, oldRef, refFor) => setRef(el, ref, oldRef, refFor, instance)
3838
}
3939

4040
/**
4141
* Function for handling a template ref
4242
*/
4343
export function setRef(
44-
instance: VaporComponentInstance,
4544
el: RefEl,
4645
ref: NodeRef,
4746
oldRef?: NodeRef,
4847
refFor = false,
48+
instance?: VaporComponentInstance,
4949
): NodeRef | undefined {
50-
if (!instance || instance.isUnmounted) return
50+
const _isString = isString(ref)
51+
if (_isString && (!instance || instance.isUnmounted)) return
5152

52-
const setupState: any = __DEV__ ? instance.setupState || {} : null
53+
const setupState: any = __DEV__
54+
? (instance && instance.setupState) || {}
55+
: null
5356
const refValue = getRefValue(el)
5457

55-
const refs =
56-
instance.refs === EMPTY_OBJ ? (instance.refs = {}) : instance.refs
58+
const refs = instance
59+
? instance.refs === EMPTY_OBJ
60+
? (instance.refs = {})
61+
: instance.refs
62+
: {}
5763

5864
// dynamic ref changed. unset old ref
5965
if (oldRef != null && oldRef !== ref) {
@@ -79,7 +85,6 @@ export function setRef(
7985
// TODO this gets called repeatedly in renderEffect when it's dynamic ref?
8086
onScopeDispose(() => invokeRefSetter())
8187
} else {
82-
const _isString = isString(ref)
8388
const _isRef = isRef(ref)
8489
let existing: unknown
8590

0 commit comments

Comments
 (0)