| 1 |
/** SSR-safe ID generation component using render props. |
| 2 |
* For function components, prefer the useSSRSafeId hook instead. |
| 3 |
* |
| 4 |
* Example of how to use this component |
| 5 |
* |
| 6 |
* const Component = ({id}: {id: string}) => ( |
| 7 |
* <GenerateId>{randomId => ( |
| 8 |
* <div id={id || randomId}> |
| 9 |
* div with random ID |
| 10 |
* </div> |
| 11 |
* )} |
| 12 |
* </GenerateId> |
| 13 |
* ); |
| 14 |
*/ |
| 15 |
import { useSSRSafeId } from '../useSSRSafeId'; |
| 16 |
|
| 17 |
export interface GenerateIdProps { |
| 18 |
/** String to prefix the id with */ |
| 19 |
prefix?: string; |
| 20 |
/** Component to be rendered with the generated id */ |
| 21 |
children(id: string): React.ReactNode; |
| 22 |
/** @deprecated Has no effect. Kept for backward compatibility. */ |
| 23 |
isRandom?: boolean; |
| 24 |
} |
| 25 |
|
| 26 |
const GenerateId = ({ prefix = 'pf-random-id-', children }: GenerateIdProps) => { |
| 27 |
const id = useSSRSafeId(prefix); |
| 28 |
return children(id) as React.ReactElement; |
| 29 |
}; |
| 30 |
GenerateId.displayName = 'GenerateId'; |
| 31 |
|
| 32 |
export { GenerateId }; |