@zedux/react
Version:
A Molecular State Engine for React
47 lines (46 loc) • 2.75 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useReactComponentId = void 0;
const react_1 = __importStar(require("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.
*/
const useReactComponentId = () => {
var _a, _b, _c, _d, _e, _f, _g;
const component = ((_d = (_c = (_b = (_a = (react_1.default[clientKey] || react_1.default[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_1.default.__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}-${(0, react_1.useId)()}`;
};
exports.useReactComponentId = useReactComponentId;