function useCutoff(x, cutoff) { // track each value of x between renders let xPrev = useRef(x); useEffect(() => { xPrev.current = x }, [x]); // if the cutoff function returns true, render the previously returned value. // else, synchronously schedule render withnew state value let [ret, setRet] = useState(x); if (!cutoff(xPrev.current, x)) { setRet(x) } return ret; } // Example usage: // maintain current value of x if it is not increasing let x = useCutoff(x, (prev, cur) => cur - prev <= 0);