-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# `useInterval` | ||
|
||
React hook that allow you using declarative `setInterval`. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import * as React from 'react'; | ||
import useInterval from 'react-use/lib/useInterval'; | ||
|
||
const Demo = () => { | ||
const [count, setCount] = React.useState(0); | ||
const [delay, setDelay] = React.useState(1000); | ||
|
||
useInterval(() => { | ||
setCount(count + 1); | ||
}, delay); | ||
|
||
function handleDelayChange(e) { | ||
setDelay(Number(e.target.value)); | ||
} | ||
|
||
return ( | ||
<div> | ||
<div> | ||
delay: <input value={delay} onChange={handleDelayChange} /> | ||
</div> | ||
<h1>count: {count}</h1> | ||
<div> | ||
<button onClick={() => setDelay(delay ? null : 1000)}>{delay ? 'stop' : 'start'}</button> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
|
||
## Reference | ||
|
||
```js | ||
useInterval(fn, delay?: number) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { storiesOf } from '@storybook/react'; | ||
import * as React from 'react'; | ||
import { useInterval } from '..'; | ||
import ShowDocs from './util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const [count, setCount] = React.useState(0); | ||
const [delay, setDelay] = React.useState(1000); | ||
|
||
useInterval(() => { | ||
setCount(count + 1); | ||
}, delay); | ||
|
||
function handleDelayChange(e) { | ||
setDelay(Number(e.target.value)); | ||
} | ||
|
||
return ( | ||
<div> | ||
<div> | ||
delay: <input value={delay} onChange={handleDelayChange} /> | ||
</div> | ||
<h1>count: {count}</h1> | ||
<div> | ||
<button onClick={() => setDelay(delay ? null : 1000)}>{delay ? 'stop' : 'start'}</button> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf('Side effects|useInterval', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useInterval.md')} />) | ||
.add('Demo', () => <Demo />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useRef, useEffect } from 'react'; | ||
|
||
const useInterval = (callback: Function, delay?: number | null) => { | ||
const latestCallback = useRef<Function>(() => {}); | ||
|
||
useEffect(() => { | ||
latestCallback.current = callback; | ||
}); | ||
|
||
useEffect(() => { | ||
if (delay !== null) { | ||
const interval = setInterval(() => latestCallback.current(), delay || 0); | ||
return () => clearInterval(interval); | ||
} | ||
return undefined; | ||
}, [delay]); | ||
}; | ||
|
||
export default useInterval; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters