Skip to content

Commit

Permalink
feat: remove compatibility under React v18
Browse files Browse the repository at this point in the history
  • Loading branch information
MadCcc committed Feb 5, 2025
1 parent bed0452 commit 7a0eef6
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 307 deletions.
48 changes: 0 additions & 48 deletions src/hooks/useCompatibleInsertionEffect.tsx

This file was deleted.

22 changes: 4 additions & 18 deletions src/hooks/useEffectCleanupRegister.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { warning } from 'rc-util/lib/warning';
import * as React from 'react';

const fullClone = {
...React,
};
const { useInsertionEffect } = fullClone;
import { useEffect } from 'react';
import type { DependencyList } from 'react';

// DO NOT register functions in useEffect cleanup function, or functions that registered will never be called.
const useCleanupRegister = (deps?: React.DependencyList) => {
const useEffectCleanupRegister = (deps?: DependencyList) => {
const effectCleanups: (() => void)[] = [];
let cleanupFlag = false;
function register(fn: () => void) {
Expand All @@ -23,7 +19,7 @@ const useCleanupRegister = (deps?: React.DependencyList) => {
effectCleanups.push(fn);
}

React.useEffect(() => {
useEffect(() => {
// Compatible with strict mode
cleanupFlag = false;
return () => {
Expand All @@ -37,14 +33,4 @@ const useCleanupRegister = (deps?: React.DependencyList) => {
return register;
};

const useRun = () => {
return function (fn: () => void) {
fn();
};
};

// Only enable register in React 18
const useEffectCleanupRegister =
typeof useInsertionEffect !== 'undefined' ? useCleanupRegister : useRun;

export default useEffectCleanupRegister;
15 changes: 4 additions & 11 deletions src/hooks/useGlobalCache.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as React from 'react';
import { pathKey, type KeyType } from '../Cache';
import StyleContext from '../StyleContext';
import useCompatibleInsertionEffect from './useCompatibleInsertionEffect';
import useEffectCleanupRegister from './useEffectCleanupRegister';
import useHMR from './useHMR';
import { useInsertionEffect } from 'react';

export type ExtractStyle<CacheValue> = (
cache: CacheValue,
Expand Down Expand Up @@ -74,20 +74,13 @@ export default function useGlobalCache<CacheType>(
const cacheContent = cacheEntity![1];

// Remove if no need anymore
useCompatibleInsertionEffect(
useInsertionEffect(
() => {
onCacheEffect?.(cacheContent);
},
(polyfill) => {
// It's bad to call build again in effect.
// But we have to do this since StrictMode will call effect twice
// which will clear cache on the first time.
buildCache(([times, cache]) => {
if (polyfill && times === 0) {
onCacheEffect?.(cacheContent);
}
return [times + 1, cache];
});
buildCache(([times, cache]) => [times + 1, cache]);

return () => {
globalCache.opUpdate(fullPathStr, (prevCache) => {
Expand All @@ -100,7 +93,7 @@ export default function useGlobalCache<CacheType>(
// With polyfill, registered callback will always be called synchronously
// But without polyfill, it will be called in effect clean up,
// And by that time this cache is cleaned up.
if (polyfill || !globalCache.opGet(fullPathStr)) {
if (!globalCache.opGet(fullPathStr)) {
onCacheRemove?.(cache, false);
}
});
Expand Down
230 changes: 0 additions & 230 deletions tests/legacy.spec.tsx

This file was deleted.

0 comments on commit 7a0eef6

Please sign in to comment.