Rooks

useFreshTick

About

Like useFreshRef but specifically for functions

Examples

import { useFreshTick } from "rooks";
import { useEffect, useState } from "react";
 
/* eslint-disable */
 
export default function App() {
  const [currentValue, setCurrentValue] = useState(0);
 
  function Increment() {
    setCurrentValue(currentValue + 1);
  }
 
  const freshTick = useFreshTick(Increment);
 
  useEffect(() => {
    const intervalId = setInterval(() => {
      freshTick();
    }, 1000);
    return () => clearInterval(intervalId);
  }, []);
  return (
    <div className="App">
      <h1> Rooks : useFreshTick example</h1>
      <h3>
        current value : <i>{currentValue} </i>
      </h3>
    </div>
  );
}

Arguments

Argument valueTypeDescription
callbackfunctionThe function call which needs to be fresh at all times

Returns

Return valueTypeDescriptionDefault value
reffunctionA function with fresh argsundefined

On this page