-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new hook useCustomCompareEffect
- Loading branch information
Showing
10 changed files
with
597 additions
and
326 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
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,8 @@ | ||
import * as React from 'react'; | ||
import { useCustomCompareEffect } from '../..'; | ||
|
||
export const Example: React.FC = () => { | ||
useCustomCompareEffect(() => {}, []); | ||
|
||
return <div>Im unable to imagine worthy example 😭😭</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,47 @@ | ||
import { Canvas, Meta, Story } from '@storybook/addon-docs/blocks'; | ||
import { Example } from './example.stories'; | ||
|
||
<Meta title="Lifecycle/useCustomCompareEffect" component={Example} /> | ||
|
||
# useCustomCompareEffect | ||
|
||
Like `useEffect` but uses provided comparator function to validate dependencies change. | ||
|
||
- SSR-friendly, meaning that comparator won't be called on the server. | ||
- Ability to change underlying effect hook (default to `useEffect`). | ||
|
||
#### Example | ||
|
||
<Canvas> | ||
<Story story={Example} inline /> | ||
</Canvas> | ||
|
||
## Reference | ||
|
||
```ts | ||
export function useCustomCompareEffect< | ||
Callback extends IEffectCallback = IEffectCallback, | ||
Deps extends DependencyList = DependencyList, | ||
HookRestArgs extends any[] = any[], | ||
R extends HookRestArgs = HookRestArgs | ||
>( | ||
callback: Callback, | ||
deps: Deps, | ||
comparator: IDependenciesComparator<Deps> = basicDepsComparator, | ||
effectHook: IEffectHook<Callback, Deps, HookRestArgs> = useEffect, | ||
...effectHookRestArgs: R | ||
): void; | ||
``` | ||
|
||
#### Arguments | ||
|
||
- **callback** _`IEffectCallback`_ - Function that will be passed to underlying effect hook. | ||
- **deps** _`React.DependencyList`_ - Dependencies list like for `useEffect`. If not undefined - | ||
effect will be triggered when deps changed AND conditions are satisfying predicate. | ||
- **comparator** _`IDependenciesComparator<Deps> `_ - Function that compares two dependencies | ||
arrays, and returns true in case they're equal. | ||
- **effectHook** _`IEffectHook<Callback, Deps, HookRestArgs>`_ - Effect hook that will be used to | ||
run callback. Must comply `useEffect` signature, meaning that callback should be placed as first | ||
argument and dependencies list as second. | ||
- **...effectHookRestArgs** _`HookRestArgs`_ - Extra arguments that passed to effectHook. Meaning | ||
the arguments that are after 2nd argument. |
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,66 @@ | ||
import { renderHook } from '@testing-library/react-hooks/dom'; | ||
import { DependencyList } from 'react'; | ||
import { IEffectCallback, useCustomCompareEffect, useUpdateEffect } from '../..'; | ||
|
||
describe('useCustomCompareEffect', () => { | ||
it('should be defined', () => { | ||
expect(useCustomCompareEffect).toBeDefined(); | ||
}); | ||
|
||
it('should render', () => { | ||
const { result } = renderHook(() => useCustomCompareEffect(() => {}, [])); | ||
expect(result.error).toBeUndefined(); | ||
}); | ||
|
||
it('should not call provided comparator on render', () => { | ||
const spy = jest.fn(); | ||
const { rerender } = renderHook(() => | ||
useCustomCompareEffect(() => {}, [], spy, useUpdateEffect) | ||
); | ||
expect(spy).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('should call comparator with previous and current deps as args', () => { | ||
const spy = jest.fn(); | ||
const { rerender } = renderHook( | ||
({ deps }) => useCustomCompareEffect(() => {}, deps, spy, useUpdateEffect), | ||
{ initialProps: { deps: [1, 2] } } | ||
); | ||
rerender({ deps: [1, 3] }); | ||
|
||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(spy.mock.calls[0][0]).toStrictEqual([1, 2]); | ||
expect(spy.mock.calls[0][1]).toStrictEqual([1, 3]); | ||
}); | ||
|
||
it('should not pass new deps to underlying effect only if comparator reported unequal deps', () => { | ||
const spy = jest.fn(useUpdateEffect); | ||
const { rerender } = renderHook( | ||
({ deps }) => useCustomCompareEffect(() => {}, deps, undefined, spy), | ||
{ initialProps: { deps: [1, 2] } } | ||
); | ||
rerender({ deps: [1, 2] }); | ||
|
||
expect(spy).toHaveBeenCalledTimes(2); | ||
expect(spy.mock.calls[0][1]).toStrictEqual([1, 2]); | ||
expect(spy.mock.calls[0][1]).toBe(spy.mock.calls[1][1]); | ||
|
||
rerender({ deps: [1, 3] }); | ||
|
||
expect(spy).toHaveBeenCalledTimes(3); | ||
expect(spy.mock.calls[2][1]).toStrictEqual([1, 3]); | ||
expect(spy.mock.calls[0][1]).not.toBe(spy.mock.calls[2][1]); | ||
}); | ||
|
||
it('should pass res argument to underlying hook', () => { | ||
const spy = jest.fn((c: IEffectCallback, d: DependencyList, _n: number) => | ||
useUpdateEffect(c, d) | ||
); | ||
const { rerender } = renderHook( | ||
({ deps }) => useCustomCompareEffect(() => {}, deps, undefined, spy, 123), | ||
{ initialProps: { deps: [1, 2] } } | ||
); | ||
|
||
expect(spy.mock.calls[0][2]).toBe(123); | ||
}); | ||
}); |
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,19 @@ | ||
import { renderHook } from '@testing-library/react-hooks/server'; | ||
import { useCustomCompareEffect } from '../..'; | ||
|
||
describe('useCustomCompareEffect', () => { | ||
it('should be defined', () => { | ||
expect(useCustomCompareEffect).toBeDefined(); | ||
}); | ||
|
||
it('should render', () => { | ||
const { result } = renderHook(() => useCustomCompareEffect(() => {}, [])); | ||
expect(result.error).toBeUndefined(); | ||
}); | ||
|
||
it('should not invoke comparator', () => { | ||
const spy = jest.fn(); | ||
renderHook(() => useCustomCompareEffect(() => {}, [], spy)); | ||
expect(spy).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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,54 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { DependencyList, useEffect, useRef } from 'react'; | ||
import { isBrowser } from '../util/const'; | ||
import { basicDepsComparator } from '../util/misc'; | ||
|
||
export type IDependenciesComparator<Deps extends DependencyList = DependencyList> = ( | ||
a: Deps, | ||
b: Deps | ||
) => boolean; | ||
|
||
export type IEffectCallback = (...args: any[]) => any; | ||
|
||
export type IEffectHook< | ||
Callback extends IEffectCallback = IEffectCallback, | ||
Deps extends DependencyList = DependencyList, | ||
RestArgs extends any[] = any[] | ||
> = ((...args: [Callback, Deps, ...RestArgs]) => void) | ((...args: [Callback, Deps]) => void); | ||
|
||
/** | ||
* Like `useEffect` but uses provided comparator function to validate dependencies change. | ||
* | ||
* @param callback Function that will be passed to underlying effect hook. | ||
* @param deps Dependencies list, like for `useEffect` hook. | ||
* @param comparator Function that compares two dependencies arrays, and returns true in case | ||
* they're equal. | ||
* @param effectHook Effect hook that will be used to run callback. Must comply `useEffect` | ||
* signature, meaning that callback should be placed as first argument and dependencies list | ||
* as second. | ||
* @param effectHookRestArgs Extra arguments that passed to effectHook. | ||
*/ | ||
export function useCustomCompareEffect< | ||
Callback extends IEffectCallback = IEffectCallback, | ||
Deps extends DependencyList = DependencyList, | ||
HookRestArgs extends any[] = any[], | ||
R extends HookRestArgs = HookRestArgs | ||
>( | ||
callback: Callback, | ||
deps: Deps, | ||
comparator: IDependenciesComparator<Deps> = basicDepsComparator, | ||
effectHook: IEffectHook<Callback, Deps, HookRestArgs> = useEffect, | ||
...effectHookRestArgs: R | ||
): void { | ||
const dependencies = useRef<Deps>(); | ||
|
||
// Effects not working in SSR environment therefore no sense to invoke comparator | ||
if ( | ||
dependencies.current === undefined || | ||
(isBrowser && !comparator(dependencies.current, deps)) | ||
) { | ||
dependencies.current = deps; | ||
} | ||
|
||
effectHook(callback, dependencies.current, ...effectHookRestArgs); | ||
} |
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
Oops, something went wrong.