(
shape: Store<State> | {[key: string]: Store<any> | any},
renderProp: (props: Props, state: State) => React.ReactNode,
)
| 8 | import {deprecate} from './deprecate' |
| 9 | |
| 10 | export function createComponent<Props, State>( |
| 11 | shape: Store<State> | {[key: string]: Store<any> | any}, |
| 12 | renderProp: (props: Props, state: State) => React.ReactNode, |
| 13 | ): StoreView<State, Props> { |
| 14 | deprecate('createComponent', '@effector/reflect') |
| 15 | let store: Store<any> |
| 16 | if (is.store(shape)) { |
| 17 | store = shape |
| 18 | } else { |
| 19 | if (typeof shape === 'object' && shape !== null) { |
| 20 | store = combine(shape) |
| 21 | } else throwError('shape should be a store or object with stores') |
| 22 | } |
| 23 | let storeName = 'Unknown' |
| 24 | //@ts-ignore |
| 25 | if (store && store.shortName) { |
| 26 | storeName = store.shortName |
| 27 | } |
| 28 | const mounted = createEvent<any>() |
| 29 | const unmounted = createEvent<any>() |
| 30 | |
| 31 | function RenderComponent(props: Props) { |
| 32 | const propsRef = React.useRef(props) |
| 33 | const state = useStore(store) |
| 34 | useIsomorphicLayoutEffect(() => { |
| 35 | mounted({props: propsRef.current, state: store.getState()}) |
| 36 | return () => { |
| 37 | unmounted({props: propsRef.current, state: store.getState()}) |
| 38 | } |
| 39 | }, []) |
| 40 | const result = renderProp(props, state) |
| 41 | propsRef.current = props |
| 42 | return result |
| 43 | } |
| 44 | RenderComponent.mounted = mounted |
| 45 | RenderComponent.unmounted = unmounted |
| 46 | return withDisplayName(`${storeName}.View`, RenderComponent) |
| 47 | } |
no test coverage detected
searching dependent graphs…