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

Function useGateBase

src/react/apiBase.ts:370–408  ·  view source on GitHub ↗
(
  GateComponent: Gate<Props>,
  props: Props = {} as any,
  scope?: Scope,
)

Source from the content-addressed store, hash-verified

368}
369
370export function useGateBase<Props>(
371 GateComponent: Gate<Props>,
372 props: Props = {} as any,
373 scope?: Scope,
374) {
375 const {open, close, set} = useUnitBase(
376 {
377 open: GateComponent.open,
378 close: GateComponent.close,
379 set: GateComponent.set,
380 },
381 scope,
382 )
383 const ForkedGate = React.useMemo(
384 () =>
385 ({
386 open,
387 close,
388 set,
389 } as Gate<Props>),
390 [GateComponent, open],
391 )
392
393 const propsRef = React.useRef<{value: any; count: number}>({
394 value: null,
395 count: 0,
396 })
397 useIsomorphicLayoutEffect(() => {
398 ForkedGate.open(propsRef.current.value)
399 return () => ForkedGate.close(propsRef.current.value) as any
400 }, [ForkedGate])
401 if (!shallowCompare(propsRef.current.value, props)) {
402 propsRef.current.value = props
403 propsRef.current.count += 1
404 }
405 useIsomorphicLayoutEffect(() => {
406 ForkedGate.set(propsRef.current.value)
407 }, [propsRef.current.count])
408}
409
410function shallowCompare(a: any, b: any) {
411 if (a === b) return true

Callers 2

useGateFunction · 0.90
useGateFunction · 0.90

Calls 2

shallowCompareFunction · 0.85
useUnitBaseFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…