-
-
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
5 changed files
with
80 additions
and
1 deletion.
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,35 @@ | ||
# `useUpdateEffect` | ||
|
||
React effect hook that ignores the first invocation (e.g. on mount). The signature is exactly the same as the `useEffect` hook. | ||
|
||
|
||
## Usage | ||
|
||
```jsx | ||
import React from 'react' | ||
import {useUpdateEffect} from 'react-use'; | ||
|
||
const Demo = () => { | ||
const [count, setCount] = React.useState(0); | ||
|
||
React.useEffect(() => { | ||
const interval = setInterval(() => { | ||
setCount(count => count + 1) | ||
}, 1000) | ||
|
||
return () => { | ||
clearInterval(interval) | ||
} | ||
}, []) | ||
|
||
useUpdateEffect(() => { | ||
console.log('count', count) // will only show 1 and beyond | ||
|
||
return () => { // *OPTIONAL* | ||
// do something on unmount | ||
} | ||
}) // you can include deps array if necessary | ||
|
||
return <div>Count: {count}</div> | ||
}; | ||
``` |
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,25 @@ | ||
import {storiesOf} from '@storybook/react'; | ||
import * as React from 'react'; | ||
import {useUpdateEffect} from '..'; | ||
|
||
const Demo = () => { | ||
const [count, setCount] = React.useState(0) | ||
const [didUpdate, setDidUpdate] = React.useState(false) | ||
|
||
useUpdateEffect(() => { | ||
setDidUpdate(true) | ||
}, [count]) | ||
|
||
return ( | ||
<div> | ||
<button onClick={() => setCount(count => count + 1)}>Count: {count}</button> | ||
<p>Updated: {didUpdate}</p> | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf('useUpdateEffect', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useUpdateEffect.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useRef, useEffect } from 'react' | ||
|
||
const useUpdateEffect: typeof useEffect = (effect, deps) => { | ||
const isInitialMount = useRef(true) | ||
|
||
useEffect( | ||
isInitialMount.current | ||
? () => { | ||
isInitialMount.current = false | ||
} | ||
: effect, | ||
deps | ||
) | ||
} | ||
|
||
export default useUpdateEffect |