mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-15 08:59:15 +08:00
19 lines
469 B
TypeScript
19 lines
469 B
TypeScript
import * as React from 'react';
|
|
import useForceUpdate from './useForceUpdate';
|
|
|
|
type UseSyncStateProps<T> = readonly [() => T, (newValue: T) => void];
|
|
|
|
export default function useSyncState<T>(initialValue: T): UseSyncStateProps<T> {
|
|
const ref = React.useRef<T>(initialValue);
|
|
const forceUpdate = useForceUpdate();
|
|
|
|
return [
|
|
() => ref.current,
|
|
(newValue: T) => {
|
|
ref.current = newValue;
|
|
// re-render
|
|
forceUpdate();
|
|
},
|
|
] as const;
|
|
}
|