import * as React from 'react' ; import { Injector , Dependency } from '@wendellhu/redi' ; import { RediProvider , RediConsumer } from './reactContext' ; function RediInjector ( props ) { const { children , dependencies } = props ; const childInjectorRef = React . useRef < Injector | null > ( null ) ; React . useEffect ( ( ) => ( ) => childInjectorRef . current ?. dispose ( ) , [ ] ) ; return ( < RediConsumer > { ( context ) => { let childInjector ; if ( childInjectorRef . current ) { childInjector = childInjectorRef . current ; } else { childInjector = context . injector ? context . injector . createChild ( dependencies ) : new Injector ( dependencies ) ; childInjectorRef . current = childInjector ; } return < RediProvider value = { { injector : childInjector } } > { children } < /RediProvider>; } } < /RediConsumer>); } param Comp param injector returns export function connectInjector < T > ( Comp , injector ) { return function ComponentWithInjector ( props ) { return ( < RediProvider value = { { injector } } > < Comp { ... props } / > < /RediProvider>); } ; } export function connectDependencies < T > ( Comp , dependencies ) { return function ComponentWithInjector ( props ) { return ( < RediInjector dependencies = { dependencies } > < Comp { ... props } / > < /RediInjector>); } ; }