-import { getCurrentInstance, SetupContext } from './component'
+import {
+ getCurrentInstance,
+ SetupContext,
+ createSetupContext
+} from './component'
import { EmitFn, EmitsOptions } from './componentEmits'
import { ComponentObjectPropsOptions, ExtractPropTypes } from './componentProps'
import { warn } from './warning'
}
export function useContext(): SetupContext {
- return getCurrentInstance()!.setupContext!
+ const i = getCurrentInstance()!
+ if (__DEV__ && !i) {
+ warn(`useContext() called without active instance.`)
+ }
+ return i.setupContext || (i.setupContext = createSetupContext(i))
}
}
}
-function createSetupContext(instance: ComponentInternalInstance): SetupContext {
+export function createSetupContext(
+ instance: ComponentInternalInstance
+): SetupContext {
const expose: SetupContext['expose'] = exposed => {
if (__DEV__ && instance.exposed) {
warn(`expose() should be called only once per setup().`)