Skip to content

Commit

Permalink
fix: backward compat with react17
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraghazra committed Jan 2, 2025
1 parent 583900a commit 9f5217d
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/blade/src/utils/useCallbackRef.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import { useCallback, useInsertionEffect, useRef } from 'react';
import React, { useCallback, useRef } from 'react';
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';

// Prevent webpack from importing this:
// https://github.com/webpack/webpack/issues/14814#issuecomment-1536757985
// https://github.com/radix-ui/primitives/issues/2796
const useReactInsertionEffect = (React as any)[' useInsertionEffect '.trim().toString()];
const useInsertionEffectFallback = useReactInsertionEffect || useIsomorphicLayoutEffect;

/**
* This hook is user-land implementation of the experimental `useEffectEvent` hook.
Expand All @@ -11,11 +19,9 @@ function useCallbackRef<Args extends unknown[], Return>(
callback: ((...args: Args) => Return) | undefined,
deps: React.DependencyList = [],
) {
const callbackRef = useRef<typeof callback>(() => {
throw new Error('Cannot call an event handler while rendering.');
});
const callbackRef = useRef<typeof callback>(callback);

useInsertionEffect(() => {
useInsertionEffectFallback(() => {
callbackRef.current = callback;
});

Expand Down

0 comments on commit 9f5217d

Please sign in to comment.