React interval hook
  • Introduction
  • Installation
  • Basics
  • Advanced
    • Definition
    • Callback
    • Interval
    • Options
      • Configuration
        • Auto start
        • Immediate callback call
        • Self correction
      • Callbacks
        • On finish
    • Hook result / Controls
      • Start
      • Stop
      • Is active
  • Examples
    • Advanced usage
    • Live demo
      • Version 1
  • Migration
    • Change log
Powered by GitBook
On this page
  1. Examples

Advanced usage

PreviousIs activeNextLive demo

Last updated 1 year ago

Was this helpful?

CtrlK

Was this helpful?

import React, { useState } from 'react';
import { useInterval } from 'react-interval-hook';

const AdvancedExample = () => {
    const { start, stop, isActive
}
=
useInterval
(
() => {
console.log('Callback every 500 ms');
},
500,
{
autoStart: false,
immediate: false,
selfCorrecting: false,
onFinish: () => {
console.log('Callback when timer is stopped');
},
}
);
const [active, setActive] = useState(isActive());
const [triggerFinishCallback, setTriggerFinishCallback] = useState(true);
return (
<div>
<button type="button" onClick={start} id="start">
Start
</button>
<button type="button" onClick={() => stop(triggerFinishCallback)} id="stop">
Stop
</button>
<button type="button" onClick={() => setActive(isActive())} id="checkActive">
Check active
</button>
<div id="active">Active: {active ? 1 : 0}</div>
<div>
<label htmlFor="trigger-finish-callback">
<input
id="trigger-finish-callback"
type="checkbox"
defaultChecked={triggerFinishCallback}
onChange={() => setTriggerFinishCallback(current => !current)}
/>
Trigger finish callback
</label>
</div>
</div>
);
};