|
Last change
on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
1.2 KB
|
| Line | |
|---|
| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.useUniqueId = useUniqueId;
|
|---|
| 7 | var _useId = require("./useId");
|
|---|
| 8 | /**
|
|---|
| 9 | * A hook that generates a unique ID. It uses React.useId() in React 18+ for SSR safety
|
|---|
| 10 | * and falls back to a client-side-only unique ID generator for older versions.
|
|---|
| 11 | *
|
|---|
| 12 | * The ID will stay the same across renders, and you can optionally provide a prefix.
|
|---|
| 13 | *
|
|---|
| 14 | * @param [prefix] - An optional prefix for the generated ID.
|
|---|
| 15 | * @param [customId] - An optional custom ID to override the generated one.
|
|---|
| 16 | * @returns The unique ID.
|
|---|
| 17 | */
|
|---|
| 18 | function useUniqueId(prefix, customId) {
|
|---|
| 19 | /*
|
|---|
| 20 | * We have to call this hook here even if we don't use the result because
|
|---|
| 21 | * rules of hooks demand that hooks are never called conditionally.
|
|---|
| 22 | */
|
|---|
| 23 | var generatedId = (0, _useId.useId)();
|
|---|
| 24 |
|
|---|
| 25 | // If a custom ID is provided, it always takes precedence.
|
|---|
| 26 | if (customId) {
|
|---|
| 27 | return customId;
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | // Apply the prefix if one was provided.
|
|---|
| 31 | return prefix ? "".concat(prefix, "-").concat(generatedId) : generatedId;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * The useUniqueId hook returns a unique ID that is either reused from external props or generated internally.
|
|---|
| 36 | * Either way the ID is now guaranteed to be present so no more nulls or undefined.
|
|---|
| 37 | */ |
|---|
Note:
See
TracBrowser
for help on using the repository browser.