MCPcopy
hub / github.com/pmndrs/react-spring / useTransition

Function useTransition

packages/core/src/hooks/useTransition.tsx:73–481  ·  view source on GitHub ↗
(
  data: unknown,
  props: UseTransitionProps | (() => any),
  deps?: any[]
)

Source from the content-addressed store, hash-verified

71 : never
72
73export function useTransition(
74 data: unknown,
75 props: UseTransitionProps | (() => any),
76 deps?: any[]
77): any {
78 const propsFn = is.fun(props) && props
79
80 const {
81 reset,
82 sort,
83 trail = 0,
84 reverse = false,
85 expires = true,
86 exitBeforeEnter = false,
87 onDestroyed,
88 ref: propsRef,
89 config: propsConfig,
90 }: UseTransitionProps<any> = propsFn ? propsFn() : props
91
92 // Return a `SpringRef` if a deps array was passed.
93 const ref = useMemo(
94 () => (propsFn || arguments.length == 3 ? SpringRef() : void 0),
95 []
96 )
97
98 // Every item has its own transition.
99 const items = toArray(data)
100 const transitions: TransitionState[] = []
101
102 // The "onRest" callbacks need a ref to the latest transitions.
103 const usedTransitions = useRef<TransitionState[] | null>(null)
104 const prevTransitions = reset ? null : usedTransitions.current
105
106 useIsomorphicLayoutEffect(() => {
107 usedTransitions.current = transitions
108 })
109
110 useOnce(() => {
111 /**
112 * If transitions exist on mount of the component
113 * then reattach their refs on-mount, this was required
114 * for react18 strict mode to work properly.
115 *
116 * StrictMode's simulated unmount detaches the controller from its ref but
117 * leaves `ctrl.ref` set, so we reset it here to let the commit phase's
118 * `replaceRef` reattach an *injected* ref. We must NOT assign the local
119 * `ref` to `ctrl.ref` — that would make a function/deps-form transition
120 * defer its enter animation as if a ref were injected (see #2287).
121 *
122 * See https://github.com/pmndrs/react-spring/issues/1890
123 */
124
125 each(transitions, t => {
126 ref?.add(t.ctrl)
127 t.ctrl.ref = undefined
128 })
129
130 // Destroy all transitions on dismount.

Callers 14

TransitionFunction · 0.90
scenarioFunction · 0.90
ComponentFunction · 0.90
HomeBlockTargetFunction · 0.85
AppFunction · 0.85
ListFunction · 0.85
MasonryFunction · 0.85
MessageHubFunction · 0.85
AppFunction · 0.85
AppFunction · 0.85

Calls 15

SpringRefFunction · 0.90
toArrayFunction · 0.90
useOnceFunction · 0.90
eachFunction · 0.90
detachRefsFunction · 0.90
callPropFunction · 0.90
useForceUpdateFunction · 0.90
getDefaultPropsFunction · 0.90
inferToFunction · 0.90
getSpringsFunction · 0.90
usePrevFunction · 0.90
hasPropsFunction · 0.90

Tested by 1

ComponentFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…