UNPKG

1.25 kBJavaScriptView Raw
1'use client';
2
3import * as React from 'react';
4/**
5 * @ignore - do not document.
6 *
7 * Use this function determine the host element correctly on the server (in a SSR context, for example Next.js)
8 */
9export function useRootElementName(parameters) {
10 const {
11 rootElementName: rootElementNameProp = '',
12 componentName
13 } = parameters;
14 const [rootElementName, setRootElementName] = React.useState(rootElementNameProp.toUpperCase());
15 if (process.env.NODE_ENV !== 'production') {
16 // eslint-disable-next-line react-hooks/rules-of-hooks
17 React.useEffect(() => {
18 if (rootElementNameProp && rootElementName !== rootElementNameProp.toUpperCase()) {
19 console.error(`useRootElementName: the \`rootElementName\` prop of ${componentName ? `the ${componentName} component` : 'a component'} expected the '${rootElementNameProp}' element, but a '${rootElementName.toLowerCase()}' was rendered instead`, 'This may cause hydration issues in an SSR context, for example in a Next.js app');
20 }
21 }, [rootElementNameProp, rootElementName, componentName]);
22 }
23 const updateRootElementName = React.useCallback(instance => {
24 setRootElementName(instance?.tagName ?? '');
25 }, []);
26 return [rootElementName, updateRootElementName];
27}
\No newline at end of file