JSPM

  • Created
  • Published
  • Downloads 2319
  • Score
    100M100P100Q122898F
  • License MIT

A react hook for using setInterval

Package Exports

  • @rooks/use-interval

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@rooks/use-interval) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@rooks/use-interval

Installation

npm install --save @rooks/use-interval

Usage

function reducer(state, action) {
  switch (action.type) {
    case "increment":
      return { count: state.count + 1 };
    default:
      return state;
  }
}

function Demo() {
  const [value, dispatcher] = useReducer(reducer, { count: 0 });

  function increment() {
    dispatcher({
      type: "increment"
    });
  }

  const { start, stop } = useInterval(() => {
    increment();
  }, 1000);

  return (
    <>
      <p>value is {value.count}</p>
      <button onClick={start}>Start</button>
      <button onClick={stop}>Stop</button>
    </>
  );
}
render(<Demo/>)

Arguments

Argument Type Description Default value
callback function Function be invoked after each interval duration undefined
intervalDuration number Duration in milliseconds after which callback is invoked undefined
startImmediate boolean Should the timer start immediately or no false

Returned Object

Returned object attributes Type Description
start function Start the interval
stop function Stop the interval
intervalId intervalId IntervalId of the interval

A react hook for using setInterval