-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from beizhedenglong/effect
feat(hooks): add useDeepEqualEffect and useShallowEqualEffect
- Loading branch information
Showing
7 changed files
with
4,616 additions
and
4,827 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,45 @@ | ||
import 'react-testing-library/cleanup-after-each' | ||
import React, { useEffect } from 'react' | ||
import { | ||
render, fireEvent, | ||
} from 'react-testing-library' | ||
import { | ||
useCounter, useDeepEqualEffect, useShallowEqualEffect, | ||
} from '../src/index' | ||
|
||
const fn = jest.fn() | ||
const shallowFn = jest.fn() | ||
const shallowFn2 = jest.fn() | ||
const deepFn = jest.fn() | ||
jest.useFakeTimers() | ||
|
||
|
||
const Example = () => { | ||
const { inc } = useCounter(0) | ||
useEffect(fn, [{ name: 'victor' }]) | ||
useShallowEqualEffect(shallowFn, [{ name: 'victor' }]) | ||
useShallowEqualEffect(shallowFn2, [{ a: { b: 1 } }]) | ||
useDeepEqualEffect(deepFn, [{ a: { b: 1 } }]) | ||
return ( | ||
<div> | ||
<button onClick={() => inc(1)}>+1</button> | ||
</div> | ||
) | ||
} | ||
const { getByText } = render(<Example />) | ||
|
||
test('effect', async () => { | ||
jest.runAllTimers() | ||
expect(fn).toBeCalledTimes(1) | ||
expect(shallowFn).toBeCalledTimes(1) | ||
expect(shallowFn2).toBeCalledTimes(1) | ||
expect(deepFn).toBeCalledTimes(1) | ||
|
||
fireEvent.click(getByText('+1')) | ||
fireEvent.click(getByText('+1')) | ||
jest.runAllTimers() | ||
expect(fn).toBeCalledTimes(3) | ||
expect(shallowFn).toBeCalledTimes(1) | ||
expect(shallowFn2).toBeCalledTimes(3) | ||
expect(deepFn).toBeCalledTimes(1) | ||
}) |
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,26 @@ | ||
import { useEffect, useRef } from 'react' | ||
import shallowEqual from 'shallowequal' | ||
import deepEqual from 'lodash.isequal' | ||
|
||
const isShallowEqual = (depsA = [], depsB = []) => { | ||
if (depsA.length !== depsB.length) { | ||
return false | ||
} | ||
return depsA.every((a, index) => shallowEqual(a, depsB[index])) | ||
} | ||
|
||
const useEqualEffect = (compareFn, f, deps) => { | ||
const ref = useRef([]) | ||
if (!compareFn(ref.current, deps)) { | ||
ref.current = deps | ||
} | ||
useEffect(f, ref.current) | ||
} | ||
|
||
export const useShallowEqualEffect = (f, deps) => { | ||
useEqualEffect(isShallowEqual, f, deps) | ||
} | ||
|
||
export const useDeepEqualEffect = (f, deps) => { | ||
useEqualEffect(deepEqual, f, deps) | ||
} |
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,36 @@ | ||
import { storiesOf } from '@storybook/react' | ||
|
||
const section = name => `Effect|${name}` | ||
|
||
storiesOf(section('useDeepEqualEffect & useShallowEqualEffect'), module) | ||
.addLiveSource('demo', ` | ||
const Example = () => { | ||
const { count, inc } = useCounter(0) | ||
React.useEffect(() => { | ||
// will be called on every render | ||
console.log("useEffect") | ||
}, [{ name: 'victor' }]) | ||
useShallowEqualEffect(() => { | ||
// only be called on first render | ||
console.log("useShallowEqualEffect1") | ||
}, [{ name: 'victor' }]) | ||
useShallowEqualEffect(() => { | ||
// will be called on every render | ||
console.log("useShallowEqualEffect2") | ||
}, [{ a: { b: 1 } }]) | ||
useDeepEqualEffect(() => { | ||
// only be called on first render | ||
console.log("useDeepEqualEffect") | ||
}, [{ a: { b: 1 } }]) | ||
return ( | ||
<div> | ||
count: {count} | ||
<button onClick={() => inc(1)}>+1</button> | ||
</div> | ||
) | ||
} | ||
return <Example /> | ||
`) |
Oops, something went wrong.