@gravity-ui/graph
Version:
Modern graph editor component
18 lines (17 loc) • 589 B
JavaScript
import { useRef, useState } from "react";
import { useFn } from "./useFn";
/*
* Calls setState only if the new value differs from the already saved.
* It is useful if the setState call occurs very often, which leads to freezes.
*/
export function useCompareState(value) {
const [stateValue, setStateValue] = useState(value);
const refValue = useRef(value);
const setValue = useFn((newValue) => {
if (newValue === refValue.current)
return;
setStateValue(newValue);
refValue.current = newValue;
});
return [stateValue, setValue];
}