@kiwicom/orbit-components
Version:
Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com's products.
50 lines (38 loc) • 1.07 kB
Markdown
# useRandomId
**If you are using version 18 (or above) of React, you should use the native React.useId hook instead of this one.**
The `useRandomId` generates unique random id.
In order to use the hook, you can simply import it like:
```jsx
import useRandomId from "@kiwicom/orbit-components/lib/hooks/useRandomId"`
```
example:
```jsx
const App = () => {
const titleId = useRandomId();
return (
<svg aria-labelledBy={titleId}>
<title id={titleId}>some title</title>
...
</svg>
);
};
```
if you need to generate multiple ids, there is a hook `useRandomSeedId`:
```jsx
import { useRandomIdSeed } from "@kiwicom/orbit-components/lib/hooks/useRandomId"`
```
which you can use like that:
```jsx
const App = () => {
const randomId = useRandomIdSeed();
const titleId = randomId("title");
const descriptionId = randomId("description");
return (
<svg aria-labelledBy={`${titleId} ${descriptionId}`}>
<title id={titleId}>some title</title>
<desc id={descriptionId}>some description</desc>
...
</svg>
);
};
```