Skip to content

Commit dddd7f0

Browse files
committed
fix: add support for react < 18 with custom useId hook. #53
1 parent 32b34c1 commit dddd7f0

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

core/src/Container.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import React, { forwardRef, useId } from 'react';
1+
import React, { forwardRef } from 'react';
22
import { NestedClose } from './comps/NestedClose';
33
import { NestedOpen } from './comps/NestedOpen';
44
import { KeyValues } from './comps/KeyValues';
5+
import { useIdCompat } from './comps/useIdCompat';
56
import { useShowToolsDispatch } from './store/ShowTools';
67

78
export interface ContainerProps<T extends object> extends React.HTMLAttributes<HTMLDivElement> {
@@ -28,7 +29,7 @@ export const Container = forwardRef(<T extends object>(props: ContainerProps<T>,
2829
...elmProps
2930
} = props;
3031
const dispatch = useShowToolsDispatch();
31-
const subkeyid = useId();
32+
const subkeyid = useIdCompat();
3233
const defaultClassNames = [className, 'w-rjv-inner'].filter(Boolean).join(' ');
3334
const reset: React.HTMLAttributes<HTMLDivElement> = {
3435
onMouseEnter: () => dispatch({ [subkeyid]: true }),

core/src/comps/KeyValues.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Fragment, useId, useRef } from 'react';
1+
import { Fragment, useRef } from 'react';
22
import { useStore } from '../store';
33
import { useExpandsStore } from '../store/Expands';
44
import { useShowToolsDispatch } from '../store/ShowTools';
@@ -10,6 +10,7 @@ import { Quote, Colon } from '../symbol';
1010
import { useHighlight } from '../utils/useHighlight';
1111
import { type SectionElementResult } from '../store/Section';
1212
import { Copied } from '../comps/Copied';
13+
import { useIdCompat } from '../comps/useIdCompat';
1314

1415
interface KeyValuesProps<T extends object> extends SectionElementResult<T> {
1516
expandKey?: string;
@@ -84,7 +85,7 @@ KayName.displayName = 'JVR.KayName';
8485
export const KeyValuesItem = <T extends object>(props: KeyValuesProps<T>) => {
8586
const { keyName, value, parentValue, level = 0, keys = [] } = props;
8687
const dispatch = useShowToolsDispatch();
87-
const subkeyid = useId();
88+
const subkeyid = useIdCompat();
8889
const isMyArray = Array.isArray(value);
8990
const isMySet = value instanceof Set;
9091
const isMyMap = value instanceof Map;

core/src/comps/useIdCompat.tsx

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { useRef } from 'react';
2+
3+
export function useIdCompat() {
4+
const idRef = useRef<string | null>(null);
5+
if (idRef.current === null) {
6+
idRef.current = 'custom-id-' + Math.random().toString(36).substr(2, 9);
7+
}
8+
return idRef.current;
9+
}

0 commit comments

Comments
 (0)