-
-
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
99 additions
and
2 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,30 @@ | ||
# `useDeepCompareEffect` | ||
|
||
A modified useEffect hook that is using deep comparison on its dependencies instead of reference equality. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import {useCounter, useDeepCompareEffect} from 'react-use'; | ||
|
||
const Demo = () => { | ||
const [count, {inc: inc}] = useCounter(0); | ||
const options = { step: 2 }; | ||
|
||
useDeepCompareEffect(() => { | ||
inc(options.step) | ||
}, [options]); | ||
|
||
return ( | ||
<div> | ||
<p>useDeepCompareEffect: {count}</p> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
## Reference | ||
|
||
```ts | ||
useDeepCompareEffect(effect: () => void | (() => void | undefined), deps: any[]); | ||
``` |
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,33 @@ | ||
import {storiesOf} from '@storybook/react'; | ||
import * as React from 'react'; | ||
import {useCounter, useDeepCompareEffect} from '..'; | ||
import ShowDocs from './util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const [countNormal, {inc: incNormal}] = useCounter(0); | ||
const [countDeep, {inc: incDeep}] = useCounter(0); | ||
const options = {max: 500} | ||
|
||
React.useEffect(() => { | ||
if (countNormal < options.max) { | ||
incNormal() | ||
} | ||
}, [options]); | ||
|
||
useDeepCompareEffect(() => { | ||
if (countNormal < options.max) { | ||
incDeep() | ||
} | ||
}, [options]); | ||
|
||
return ( | ||
<div> | ||
<p>useEffect: {countNormal}</p> | ||
<p>useDeepCompareEffect: {countDeep}</p> | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf('useDeepCompareEffect', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useDeepCompareEffect.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,30 @@ | ||
import { useRef, useEffect, EffectCallback, DependencyList } from 'react'; | ||
import * as isEqual from 'react-fast-compare'; | ||
|
||
const isPrimitive = (val: any) => val !== Object(val) | ||
|
||
const useDeepCompareEffect = (effect: EffectCallback, deps: any[]) => { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (!deps || !deps.length) { | ||
console.warn( | ||
'`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead.' | ||
); | ||
} | ||
|
||
if (deps.every(isPrimitive)) { | ||
console.warn( | ||
'`useDeepCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead.' | ||
); | ||
} | ||
} | ||
|
||
const ref = useRef<DependencyList | undefined>(undefined); | ||
|
||
if (!isEqual(deps, ref.current)) { | ||
ref.current = deps | ||
} | ||
|
||
useEffect(effect, ref.current) | ||
} | ||
|
||
export default useDeepCompareEffect |
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