fix(collapse): fix infinite loop caused by defaultValue as an array

This commit is contained in:
xzdry 2023-05-05 17:33:38 +08:00
parent 75eb138e7e
commit 4a145ba7f2
2 changed files with 20 additions and 1 deletions

View 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;

View File

@ -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 });