11 lines
330 B
JavaScript
11 lines
330 B
JavaScript
|
|
import * as React from 'react';
|
||
|
|
import { useForceUpdate } from './useForceUpdate';
|
||
|
|
export const useSyncState = initialValue => {
|
||
|
|
const ref = React.useRef(initialValue);
|
||
|
|
const [, forceUpdate] = useForceUpdate();
|
||
|
|
return [() => ref.current, newValue => {
|
||
|
|
ref.current = newValue;
|
||
|
|
// re-render
|
||
|
|
forceUpdate();
|
||
|
|
}];
|
||
|
|
};
|