-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: usedeepcomparecallback with useDeepCompareMemoize
- Loading branch information
Showing
5 changed files
with
177 additions
and
102 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,13 @@ | ||
import { useDeepCompareMemoize } from 'use-deep-compare-effect' | ||
import React from 'react' | ||
|
||
type UseCallbackParameters = Parameters<typeof React.useCallback> | ||
type UseCallbackCallback = UseCallbackParameters[0] | ||
type DependencyList = UseCallbackParameters[1] | ||
|
||
export function useDeepCompareCallback<T extends UseCallbackCallback>( | ||
callback: T, | ||
deps: DependencyList | ||
) { | ||
return React.useCallback(callback, useDeepCompareMemoize(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
56 changes: 56 additions & 0 deletions
56
packages/graphql-hooks/test-jsdom/unit/useDeepCompareCallback.test.ts
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,56 @@ | ||
import { renderHook } from '@testing-library/react' | ||
import { useDeepCompareCallback } from '../../src/useDeepCompareCallback' | ||
|
||
function renderHookWithDeps<T, U>(callback: (props: T) => U, props: T) { | ||
return renderHook(callback, { | ||
initialProps: props | ||
}) | ||
} | ||
|
||
describe('useDeepCompareCallback', () => { | ||
it('should get the same instance if the deps is an empty array and rerender an empty array', () => { | ||
const { result, rerender } = renderHookWithDeps( | ||
({ deps }) => | ||
useDeepCompareCallback(() => { | ||
return deps | ||
}, deps), | ||
{ | ||
deps: [] | ||
} | ||
) | ||
const firstResult = result.current | ||
rerender({ deps: [] }) | ||
expect(result.current).toEqual(firstResult) | ||
}) | ||
|
||
it('should get the same instance if the deps is defined and rerender with a clone of the deps', () => { | ||
const deps = [{ a: 1 }, { b: 1 }] | ||
const { result, rerender } = renderHookWithDeps( | ||
({ deps }) => | ||
useDeepCompareCallback(() => { | ||
return deps | ||
}, deps), | ||
{ | ||
deps | ||
} | ||
) | ||
const firstResult = result.current | ||
rerender({ deps: structuredClone(deps) }) | ||
expect(result.current).toEqual(firstResult) | ||
}) | ||
|
||
it('should get a new instance if the deps is changed', () => { | ||
const { result, rerender } = renderHookWithDeps( | ||
({ deps }) => | ||
useDeepCompareCallback(() => { | ||
return deps | ||
}, deps), | ||
{ | ||
deps: [{ a: 1 }] | ||
} | ||
) | ||
const firstResult = result.current | ||
rerender({ deps: [{ a: 2 }] }) | ||
expect(result.current).not.toEqual(firstResult) | ||
}) | ||
}) |