From 27e4444dd3740cc60e642c04e3226313dad9bc3a Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Thu, 21 Mar 2019 10:07:41 -0300 Subject: [PATCH 1/5] Create useUpdateEffect.ts --- src/useUpdateEffect.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/useUpdateEffect.ts diff --git a/src/useUpdateEffect.ts b/src/useUpdateEffect.ts new file mode 100644 index 0000000000..e696b034fb --- /dev/null +++ b/src/useUpdateEffect.ts @@ -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 From 7ca74e6aad424a10a9147d27aebdfe5ac0175412 Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Thu, 21 Mar 2019 10:12:01 -0300 Subject: [PATCH 2/5] Create useUpdateEffect.md --- docs/useUpdateEffect.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 docs/useUpdateEffect.md diff --git a/docs/useUpdateEffect.md b/docs/useUpdateEffect.md new file mode 100644 index 0000000000..ca3c7f6d75 --- /dev/null +++ b/docs/useUpdateEffect.md @@ -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
Count: {count}
+}; +``` From d7f9ee4cb388b3b15f9b2be2809c4d4012749676 Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Thu, 21 Mar 2019 10:14:31 -0300 Subject: [PATCH 3/5] Update index.ts --- src/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 66bdf580d9..6fb4a79f76 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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, @@ -101,5 +102,6 @@ export { useUpdate, useVideo, useWindowSize, - useWait + useWait, + useUpdateEffect }; From 257d8c074a6ebb24beef5a3bf4bc54bfd6ec5565 Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Thu, 21 Mar 2019 10:15:42 -0300 Subject: [PATCH 4/5] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2e89bc68a1..8d2f0faab1 100644 --- a/README.md +++ b/README.md @@ -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.

- [**State**](./docs/State.md) From 26953f12cf48a4793ebca1b34839c204b0f811b2 Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Thu, 21 Mar 2019 10:20:36 -0300 Subject: [PATCH 5/5] Create useUpdateEffect.story.tsx --- src/__stories__/useUpdateEffect.story.tsx | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/__stories__/useUpdateEffect.story.tsx diff --git a/src/__stories__/useUpdateEffect.story.tsx b/src/__stories__/useUpdateEffect.story.tsx new file mode 100644 index 0000000000..28650a59bb --- /dev/null +++ b/src/__stories__/useUpdateEffect.story.tsx @@ -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 ( +
+ +

Updated: {didUpdate}

+
+ ); +}; + +storiesOf('useUpdateEffect', module) + .add('Docs', () => ) + .add('Demo', () => + + )