Skip to content

Commit 7a0eef6

Browse files
committed
feat: remove compatibility under React v18
1 parent bed0452 commit 7a0eef6

File tree

4 files changed

+8
-307
lines changed

4 files changed

+8
-307
lines changed

src/hooks/useCompatibleInsertionEffect.tsx

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import { warning } from 'rc-util/lib/warning';
2-
import * as React from 'react';
3-
4-
const fullClone = {
5-
...React,
6-
};
7-
const { useInsertionEffect } = fullClone;
2+
import { useEffect } from 'react';
3+
import type { DependencyList } from 'react';
84

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

26-
React.useEffect(() => {
22+
useEffect(() => {
2723
// Compatible with strict mode
2824
cleanupFlag = false;
2925
return () => {
@@ -37,14 +33,4 @@ const useCleanupRegister = (deps?: React.DependencyList) => {
3733
return register;
3834
};
3935

40-
const useRun = () => {
41-
return function (fn: () => void) {
42-
fn();
43-
};
44-
};
45-
46-
// Only enable register in React 18
47-
const useEffectCleanupRegister =
48-
typeof useInsertionEffect !== 'undefined' ? useCleanupRegister : useRun;
49-
5036
export default useEffectCleanupRegister;

src/hooks/useGlobalCache.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as React from 'react';
22
import { pathKey, type KeyType } from '../Cache';
33
import StyleContext from '../StyleContext';
4-
import useCompatibleInsertionEffect from './useCompatibleInsertionEffect';
54
import useEffectCleanupRegister from './useEffectCleanupRegister';
65
import useHMR from './useHMR';
6+
import { useInsertionEffect } from 'react';
77

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

7676
// Remove if no need anymore
77-
useCompatibleInsertionEffect(
77+
useInsertionEffect(
7878
() => {
7979
onCacheEffect?.(cacheContent);
80-
},
81-
(polyfill) => {
8280
// It's bad to call build again in effect.
8381
// But we have to do this since StrictMode will call effect twice
8482
// which will clear cache on the first time.
85-
buildCache(([times, cache]) => {
86-
if (polyfill && times === 0) {
87-
onCacheEffect?.(cacheContent);
88-
}
89-
return [times + 1, cache];
90-
});
83+
buildCache(([times, cache]) => [times + 1, cache]);
9184

9285
return () => {
9386
globalCache.opUpdate(fullPathStr, (prevCache) => {
@@ -100,7 +93,7 @@ export default function useGlobalCache<CacheType>(
10093
// With polyfill, registered callback will always be called synchronously
10194
// But without polyfill, it will be called in effect clean up,
10295
// And by that time this cache is cleaned up.
103-
if (polyfill || !globalCache.opGet(fullPathStr)) {
96+
if (!globalCache.opGet(fullPathStr)) {
10497
onCacheRemove?.(cache, false);
10598
}
10699
});

tests/legacy.spec.tsx

Lines changed: 0 additions & 230 deletions
This file was deleted.

0 commit comments

Comments
 (0)