Skip to content

Commit f76a724

Browse files
committed
fix(useSafeState): 修复 React 18 版本检测问题
1 parent b0c3236 commit f76a724

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

packages/react-use/src/use-safe-state/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export function useSafeState<T>(initialState?: Gettable<T>, options?: UseSafeSta
4141
/**
4242
* @see https://github.com/reactwg/react-18/discussions/82
4343
*/
44-
if (!isReact18OrLater && isUnmounted()) return
44+
// React < 18, and the component is unmounted, return as noop
45+
if (!isReact18OrLater() && isUnmounted()) return
4546

4647
const { deep } = latest.current
4748

packages/react-use/src/utils/basic.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,13 @@ export function isObject(val: unknown): val is object {
106106
}
107107

108108
export function isReact18OrLater(): boolean {
109-
return Number(ReactVersion?.split('.')[0].trim()) >= 18
109+
if (!ReactVersion || !isString(ReactVersion)) {
110+
return false
111+
}
112+
113+
const mainVersion = Number(ReactVersion.split('.')[0].trim())
114+
115+
return Number.isNaN(mainVersion) ? false : mainVersion >= 18
110116
}
111117

112118
export function hasOwn<T extends object, K extends PropertyKey>(val: T, key: K): boolean {

0 commit comments

Comments
 (0)