MCPcopy Create free account
hub / github.com/effector/effector / createComponent

Function createComponent

src/react/createComponent.ts:10–47  ·  view source on GitHub ↗
(
  shape: Store<State> | {[key: string]: Store<any> | any},
  renderProp: (props: Props, state: State) => React.ReactNode,
)

Source from the content-addressed store, hash-verified

8import {deprecate} from './deprecate'
9
10export 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}

Callers 1

createStoreConsumerFunction · 0.90

Calls 6

deprecateFunction · 0.90
combineFunction · 0.90
throwErrorFunction · 0.90
createEventFunction · 0.90
withDisplayNameFunction · 0.90
storeMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…