MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useCounter

Function useCounter

packages/react-aria/src/ssr/SSRProvider.tsx:112–156  ·  view source on GitHub ↗
(isDisabled = false)

Source from the content-addressed store, hash-verified

110let componentIds = new WeakMap();
111
112function useCounter(isDisabled = false) {
113 let ctx = useContext(SSRContext);
114 let ref = useRef<number | null>(null);
115 // eslint-disable-next-line rsp-rules/pure-render
116 // oxlint-disable-next-line react/react-compiler, rsp-rules/pure-render
117 if (ref.current === null && !isDisabled) {
118 // In strict mode, React renders components twice, and the ref will be reset to null on the second render.
119 // This means our id counter will be incremented twice instead of once. This is a problem because on the
120 // server, components are only rendered once and so ids generated on the server won't match the client.
121 // In React 18, useId was introduced to solve this, but it is not available in older versions. So to solve this
122 // we need to use some React internals to access the underlying Fiber instance, which is stable between renders.
123 // This is exposed as ReactCurrentOwner in development, which is all we need since StrictMode only runs in development.
124 // To ensure that we only increment the global counter once, we store the starting id for this component in
125 // a weak map associated with the Fiber. On the second render, we reset the global counter to this value.
126 // Since React runs the second render immediately after the first, this is safe.
127 let currentOwner =
128 // @ts-ignore
129 React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED?.ReactCurrentOwner?.current;
130 if (currentOwner) {
131 let prevComponentValue = componentIds.get(currentOwner);
132 if (prevComponentValue == null) {
133 // On the first render, and first call to useId, store the id and state in our weak map.
134 componentIds.set(currentOwner, {
135 id: ctx.current,
136 state: currentOwner.memoizedState
137 });
138 } else if (currentOwner.memoizedState !== prevComponentValue.state) {
139 // On the second render, the memoizedState gets reset by React.
140 // Reset the counter, and remove from the weak map so we don't
141 // do this for subsequent useId calls.
142 // oxlint-disable-next-line react/react-compiler
143 ctx.current = prevComponentValue.id;
144 componentIds.delete(currentOwner);
145 }
146 }
147
148 // eslint-disable-next-line rsp-rules/pure-render
149 // oxlint-disable-next-line react/react-compiler, rsp-rules/pure-render
150 ref.current = ++ctx.current;
151 }
152
153 // eslint-disable-next-line rsp-rules/pure-render
154 // oxlint-disable-next-line react/react-compiler, rsp-rules/pure-render
155 return ref.current;
156}
157
158function useLegacySSRSafeId(defaultId?: string): string {
159 let ctx = useContext(SSRContext);

Callers 2

LegacySSRProviderFunction · 0.85
useLegacySSRSafeIdFunction · 0.85

Calls 2

deleteMethod · 0.80
setMethod · 0.45

Tested by

no test coverage detected