Skip to content

Commit

Permalink
refactor: SafeReact util
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiks committed Apr 9, 2024
1 parent a00a6e9 commit 7f36f14
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
6 changes: 2 additions & 4 deletions packages/react/src/hooks/useId.ts
@@ -1,8 +1,6 @@
import * as React from 'react';
import useModernLayoutEffect from 'use-isomorphic-layout-effect';

// https://github.com/mui/material-ui/issues/41190#issuecomment-2040873379
const _React = {...React};
import {SafeReact} from '../utils/safeReact';

let serverHandoffComplete = false;
let count = 0;
Expand Down Expand Up @@ -30,7 +28,7 @@ function useFloatingId() {
return id;
}

const useReactId = _React.useId as () => string;
const useReactId = SafeReact.useId as () => string;

/**
* Uses React 18's built-in `useId()` when available, or falls back to a
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/hooks/utils/useEffectEvent.ts
@@ -1,9 +1,9 @@
import * as React from 'react';
import {SafeReact} from '../../utils/safeReact';

type AnyFunction = (...args: any[]) => any;

// `toString()` prevents bundlers from trying to `import { useInsertionEffect } from 'react'`
const useInsertionEffect = (React as any)['useInsertionEffect'.toString()] as
const useInsertionEffect = SafeReact.useInsertionEffect as
| AnyFunction
| undefined;

Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/utils/safeReact.ts
@@ -0,0 +1,4 @@
import * as React from 'react';

// https://github.com/mui/material-ui/issues/41190#issuecomment-2040873379
export const SafeReact = {...React};

0 comments on commit 7f36f14

Please sign in to comment.