mirror of
https://github.com/smartxworks/sunmao-ui.git
synced 2024-11-21 03:15:49 +08:00
fix(collapse): fix infinite loop caused by defaultValue as an array
This commit is contained in:
parent
75eb138e7e
commit
4a145ba7f2
18
packages/arco-lib/src/hooks/useDeepCompareEffect.ts
Normal file
18
packages/arco-lib/src/hooks/useDeepCompareEffect.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
import isEqual from 'lodash/isEqual';
|
||||
|
||||
const useDeepCompareEffect = (
|
||||
effect: React.EffectCallback,
|
||||
deps: React.DependencyList
|
||||
) => {
|
||||
const prevDeps = useRef<React.DependencyList | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (prevDeps.current && !isEqual(deps, prevDeps.current)) {
|
||||
effect();
|
||||
}
|
||||
prevDeps.current = deps;
|
||||
}, [deps, effect]);
|
||||
};
|
||||
|
||||
export default useDeepCompareEffect;
|
@ -1,6 +1,7 @@
|
||||
import { RuntimeFunctions } from '@sunmao-ui/runtime';
|
||||
import { useState, useEffect } from 'react';
|
||||
import { SlotSpec } from '@sunmao-ui/core';
|
||||
import useDeepCompareEffect from './useDeepCompareEffect';
|
||||
|
||||
export const useStateValue = <
|
||||
T,
|
||||
@ -22,7 +23,7 @@ export const useStateValue = <
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
useDeepCompareEffect(() => {
|
||||
if (updateWhenDefaultValueChanges && mergeState) {
|
||||
setValue(defaultValue);
|
||||
mergeState({ [key]: defaultValue });
|
||||
|
Loading…
Reference in New Issue
Block a user