|
1 | 1 | # :watch: React Interval Hook |
2 | 2 |
|
3 | | -> React hook for using **self-correcting** `setInterval`, augmented by management methods (start, stop, isActive) |
4 | | -
|
5 | | -[](https://travis-ci.org/minwork/react-interval-hook) |
6 | | -[](https://codecov.io/gh/minwork/react-interval-hook) |
| 3 | +[](https://codecov.io/gh/minwork/react) |
7 | 4 |  |
8 | 5 | [](https://bundlephobia.com/result?p=react-interval-hook) |
9 | 6 | [](https://www.npmjs.com/package/react-interval-hook) |
10 | | -[](https://github.com/minwork/react-interval-hook/blob/master/LICENSE) |
| 7 | + |
| 8 | +> React hook for using **self-correcting** `setInterval`, augmented by management methods |
| 9 | +
|
| 10 | +## Main features |
11 | 11 |
|
12 | 12 | - Self-correcting ([explanation](https://stackoverflow.com/a/29972322/10322539)) |
13 | 13 | - Manageable (start, stop, isActive) |
|
25 | 25 | npm install --save react-interval-hook |
26 | 26 | ``` |
27 | 27 |
|
28 | | -## Basic Usage |
29 | | - |
30 | | -```typescript jsx |
31 | | -import React from 'react'; |
32 | | -import { useInterval } from 'react-interval-hook'; |
33 | | - |
34 | | -const Example = () => { |
35 | | - useInterval(() => { |
36 | | - console.log('I am called every second'); |
37 | | - }); |
38 | | -}; |
39 | | -``` |
40 | | - |
41 | | -## Advanced usage |
42 | | - |
43 | | -Hook can accept various config option as well as return methods that allow you to control it behaviour. |
44 | | - |
45 | | -### Definition |
46 | | - |
47 | | -``` |
48 | | -useInterval(callback [, intervalMs] [, options]): { start, stop, isActive } |
49 | | -``` |
50 | | - |
51 | | -### Example |
52 | | - |
53 | | -[](https://codesandbox.io/s/react-interval-hook-bi0kx?fontsize=14&hidenavigation=1&theme=dark) |
54 | | - |
55 | | -```typescript jsx |
56 | | -import React, { useState } from 'react'; |
57 | | -import { useInterval } from 'react-interval-hook'; |
58 | | - |
59 | | -const AdvancedExample = () => { |
60 | | - const { start, stop, isActive } = useInterval( |
61 | | - () => { |
62 | | - console.log('Callback every 500 ms'); |
63 | | - }, |
64 | | - 500, |
65 | | - { |
66 | | - autoStart: false, |
67 | | - immediate: false, |
68 | | - selfCorrecting: false, |
69 | | - onFinish: () => { |
70 | | - console.log('Callback when timer is stopped'); |
71 | | - }, |
72 | | - } |
73 | | - ); |
74 | | - const [active, setActive] = useState(isActive()); |
75 | | - const [triggerFinishCallback, setTriggerFinishCallback] = useState(true); |
76 | | - |
77 | | - return ( |
78 | | - <div> |
79 | | - <button type="button" onClick={start} id="start"> |
80 | | - Start |
81 | | - </button> |
82 | | - <button type="button" onClick={() => stop(triggerFinishCallback)} id="stop"> |
83 | | - Stop |
84 | | - </button> |
85 | | - <button type="button" onClick={() => setActive(isActive())} id="checkActive"> |
86 | | - Check active |
87 | | - </button> |
88 | | - <div id="active">Active: {active ? 1 : 0}</div> |
89 | | - <div> |
90 | | - <label htmlFor="trigger-finish-callback"> |
91 | | - <input |
92 | | - id="trigger-finish-callback" |
93 | | - type="checkbox" |
94 | | - defaultChecked={triggerFinishCallback} |
95 | | - onChange={() => setTriggerFinishCallback(current => !current)} |
96 | | - /> |
97 | | - Trigger finish callback |
98 | | - </label> |
99 | | - </div> |
100 | | - </div> |
101 | | - ); |
102 | | -}; |
103 | | -``` |
104 | | - |
105 | | -### Options |
106 | | - |
107 | | -| Name | Type | Default | Description | |
108 | | -| ----------- | :------: | :------: | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
109 | | -| autoStart | boolean | true | Start interval timer right after component is mounted | |
110 | | -| immediate | boolean | false | Trigger _callback_ immediately after timer is started | |
111 | | -| selfCorrecting | boolean | true | Self correct time intervals between subsequent _callback_ invocations to reflect actual time elapsed (setInterval and setTimeout are not accurate and tend to drift). | |
112 | | -| onFinish | Function | () => {} | Called after timer is stopped (by _stop_ method or component unmount) | |
| 28 | +# Documentation |
113 | 29 |
|
114 | | -### Management methods |
| 30 | +Full documentation can be found [here](https://minwork.gitbook.io/react-interval-hook/) |
115 | 31 |
|
116 | | -`useInterval` hook return object with various management methods |
| 32 | +# Support |
117 | 33 |
|
118 | | -| Name | Arguments | Return | Description | |
119 | | -| -------- | ----------------------------------------------------------------------------- | :-----: | -------------------------------------------------------------------------------------------------- | |
120 | | -| start | None | void | Starts timer when _autoStart_ is set to `false` or after timer was stopped using _stop_ method | |
121 | | -| stop | [optional] triggerFinishCallback<br/>- Type: boolean<br/>- Default: true | void | Stops timer (**not pause**) after it was started using either _autoStart_ option or _start_ method | |
122 | | -| isActive | None | boolean | Return current timer status - is it running or not | |
| 34 | +If you like my work, consider making a [donation](https://github.com/sponsors/minwork) through Github Sponsors. |
123 | 35 |
|
124 | | -## License |
| 36 | +# License |
125 | 37 |
|
126 | 38 | MIT © [minwork](https://github.com/minwork) |
0 commit comments