Skip to content

Commit

Permalink
feat: add useUpdateEffect hook
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 21, 2019
2 parents c8f3198 + 26953f1 commit c2afd23
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
- [`useLogger`](./docs/useLogger.md) — logs in console as component goes through life-cycles.
- [`useMount`](./docs/useMount.md) — calls `mount` callbacks.
- [`useUnmount`](./docs/useUnmount.md) — calls `unmount` callbacks.
- [`useUpdateEffect`](./docs/useUpdateEffect.md) — run an `effect` only on updates.
<br/>
<br/>
- [**State**](./docs/State.md)
Expand Down
35 changes: 35 additions & 0 deletions docs/useUpdateEffect.md
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>
};
```
25 changes: 25 additions & 0 deletions src/__stories__/useUpdateEffect.story.tsx
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/>
)
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import useUpdate from './useUpdate';
import useVideo from './useVideo';
import useWindowSize from './useWindowSize';
import useWait from './useWait';
import useUpdateEffect from './useUpdateEffect'

export {
createMemo,
Expand Down Expand Up @@ -101,5 +102,6 @@ export {
useUpdate,
useVideo,
useWindowSize,
useWait
useWait,
useUpdateEffect
};
16 changes: 16 additions & 0 deletions src/useUpdateEffect.ts
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

0 comments on commit c2afd23

Please sign in to comment.