package
A React context & hook for managing and asserting the uniqueness of explicitly defined DOM IDs.
npm install react-element-idGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Estimated unpacked size across 1 packages.
10.0 kB
Includes dependencies.
A React context & hook for managing and asserting the uniqueness of explicitly defined DOM
ids.
The hook performs the following operations automatically:
[!IMPORTANT] If your app integrates multiple
Documents, you'll need to create and provide a registry for each since theirids do not overlap.
To install:
npm i react-element-idTo import into your SPA:
import { createElementIdRegistry, ElementIdRegistryContext, WARN } from 'react-element-id';
// This will not directly break your app if a duplicate is found at runtime
const registry = createElementIdRegistry(WARN);
renderToDOM(
<ElementIdRegistryContext.Provider value={registry}>
<App />
</ElementIdRegistryContext.Provider>
);To import into your SSR/SSG MPA:
import { createElementIdRegistry, ElementIdRegistryContext, THROW } from 'react-element-id';
// This will intentionally fail your build when a duplicate is found
const registry = createElementIdRegistry(THROW);
renderToString(
<ElementIdRegistryContext.Provider value={registry}>
<App />
</ElementIdRegistryContext.Provider>
);To import into your components:
import { useElementId } from 'react-element-id';
const MyComponent = ({ id }) => {
useElementId(id);
return <div id={id} />;
};
export default () => (
<>
<MyComponent id="duplicate" />
<MyComponent id="duplicate" /> {/* error */}
</>
);npm run buildThe test suite can perform a single run:
npm test… or indefinitely as files are changed:
npm run test:watch