MCPcopy
hub / github.com/mobxjs/mobx / createStoreInjector

Function createStoreInjector

packages/mobx-react/src/inject.ts:13–40  ·  view source on GitHub ↗

* Store Injection

(
    grabStoresFn: IStoresToProps,
    component: IReactComponent<any>,
    injectNames: string,
    makeReactive: boolean
)

Source from the content-addressed store, hash-verified

11 * Store Injection
12 */
13function createStoreInjector(
14 grabStoresFn: IStoresToProps,
15 component: IReactComponent<any>,
16 injectNames: string,
17 makeReactive: boolean
18): IReactComponent<any> {
19 // Support forward refs
20 let Injector: IReactComponent<any> = React.forwardRef((props, ref) => {
21 const newProps = { ...props }
22 const context = React.useContext(MobXProviderContext)
23 Object.assign(newProps, grabStoresFn(context || {}, newProps) || {})
24
25 if (ref) {
26 newProps.ref = ref
27 }
28
29 return React.createElement(component, newProps)
30 })
31
32 if (makeReactive) Injector = observer(Injector)
33 Injector["isMobxInjector"] = true // assigned late to suppress observer warning
34
35 // Static fields from component should be visible on the generated Injector
36 copyStaticProperties(component, Injector)
37 Injector["wrappedComponent"] = component
38 Injector.displayName = getInjectName(component, injectNames)
39 return Injector
40}
41
42function getInjectName(component: IReactComponent<any>, injectNames: string): string {
43 let displayName

Callers 1

injectFunction · 0.85

Calls 3

observerFunction · 0.90
copyStaticPropertiesFunction · 0.90
getInjectNameFunction · 0.85

Tested by

no test coverage detected