@zedux/react
Version:
A Molecular State Engine for React
24 lines (23 loc) • 1.67 kB
JavaScript
import React, { useId } from 'react';
const react19KeyBase = '_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE';
const clientKey = `__CLIENT${react19KeyBase}`;
const serverKey = `__SERVER${react19KeyBase}`;
/**
* Get a unique id for a Zedux hook call. This id is predictable - it should be
* exactly the same every time a given component renders in the same place in
* the component tree. This means it persists across component
* destruction/recreation which happens a lot e.g. during suspense.
*
* This uses the forbidden React internals object. We only use it to get a
* dev-friendly name for the React component's node in the atom graph. It's fine
* if React changes their internals - we'll fall back to the string "rc" ("React
* Component"). We have no need to "warn users they cannot upgrade" 'cause they
* can at the cost of some DX.
*/
export const useReactComponentId = () => {
var _a, _b, _c, _d, _e, _f, _g;
const component = ((_d = (_c = (_b = (_a = (React[clientKey] || React[serverKey])) === null || _a === void 0 ? void 0 : _a.A) === null || _b === void 0 ? void 0 : _b.getOwner) === null || _c === void 0 ? void 0 : _c.call(_b)) === null || _d === void 0 ? void 0 : _d.type) ||
((_g = (_f = (_e = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED) === null || _e === void 0 ? void 0 : _e.ReactCurrentOwner) === null || _f === void 0 ? void 0 : _f.current) === null || _g === void 0 ? void 0 : _g.type);
const name = (component === null || component === void 0 ? void 0 : component.displayName) || (component === null || component === void 0 ? void 0 : component.name) || 'rc';
return `${name}-${useId()}`;
};