import { currentInstance } from './component'
+import { currentRenderingInstance } from './componentRenderUtils'
import { warn } from './warning'
export interface InjectionKey<T> extends Symbol {}
key: InjectionKey<any> | string,
defaultValue?: unknown
) {
- if (currentInstance) {
- const provides = currentInstance.provides
+ // fallback to `currentRenderingInstance` so that this can be called in
+ // a functional component
+ const instance = currentInstance || currentRenderingInstance
+ if (instance) {
+ const provides = instance.provides
if (key in provides) {
// TS doesn't allow symbol as index type
return provides[key as string]
warn(`injection "${String(key)}" not found.`)
}
} else if (__DEV__) {
- warn(`inject() can only be used inside setup().`)
+ warn(`inject() can only be used inside setup() or functional components.`)
}
}