mirror of
https://github.com/ant-design/ant-design.git
synced 2025-08-06 16:06:28 +08:00
perf: Radio Group usePrevious instead of useState (#25122)
* perf: Radio Group usePrevious instead of useState * fix: only re-run if value changes * perf: put usePrevious custom Hook in _util/ref.ts * Generics instead of any * update
This commit is contained in:
parent
2321cc6df5
commit
09bb61e738
@ -15,3 +15,13 @@ export function composeRef<T>(...refs: React.Ref<T>[]): React.Ref<T> {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function usePrevious<T = undefined>(value: T): T | undefined {
|
||||||
|
const ref = React.useRef<T>();
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
ref.current = value;
|
||||||
|
}, [value]);
|
||||||
|
|
||||||
|
return ref.current;
|
||||||
|
}
|
||||||
|
@ -5,6 +5,7 @@ import { RadioGroupProps, RadioChangeEvent, RadioGroupButtonStyle } from './inte
|
|||||||
import { ConfigContext } from '../config-provider';
|
import { ConfigContext } from '../config-provider';
|
||||||
import SizeContext from '../config-provider/SizeContext';
|
import SizeContext from '../config-provider/SizeContext';
|
||||||
import { RadioGroupContextProvider } from './context';
|
import { RadioGroupContextProvider } from './context';
|
||||||
|
import { usePrevious } from '../_util/ref';
|
||||||
|
|
||||||
const RadioGroup: React.FC<RadioGroupProps> = props => {
|
const RadioGroup: React.FC<RadioGroupProps> = props => {
|
||||||
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
||||||
@ -17,10 +18,9 @@ const RadioGroup: React.FC<RadioGroupProps> = props => {
|
|||||||
initValue = props.defaultValue;
|
initValue = props.defaultValue;
|
||||||
}
|
}
|
||||||
const [value, setValue] = React.useState(initValue);
|
const [value, setValue] = React.useState(initValue);
|
||||||
const [prevPropValue, setPrevPropValue] = React.useState(props.value);
|
const prevPropValue = usePrevious(props.value);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
setPrevPropValue(props.value);
|
|
||||||
if (props.value !== undefined || prevPropValue !== props.value) {
|
if (props.value !== undefined || prevPropValue !== props.value) {
|
||||||
setValue(props.value);
|
setValue(props.value);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user