Skip to content

Commit

Permalink
fix(useCustomCompareMemo): Correctly infer the type of the value retu…
Browse files Browse the repository at this point in the history
…rned by the factory function

Change the return type to the type returned by the factory function instead of unknown. This makes
the hook work the same way as useMemo.

fix react-hookz#975
  • Loading branch information
ArttuOll committed Oct 22, 2022
1 parent 473be57 commit 99a7c9e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/useCustomCompareMemo/__docs__/story.mdx
Expand Up @@ -22,11 +22,11 @@ export type DependenciesComparator<Deps extends DependencyList = DependencyList>
b: Deps
) => boolean;

function useCustomCompareMemo<Factory extends () => unknown, Deps extends DependencyList>(
factory: Factory,
function useCustomCompareMemo<T, Deps extends DependencyList>(
factory: () => T,
deps: Deps,
comparator: DependenciesComparator<Deps>
): ReturnType<Factory>;
): T
```

#### Importing
Expand Down
8 changes: 4 additions & 4 deletions src/useCustomCompareMemo/useCustomCompareMemo.ts
Expand Up @@ -10,17 +10,17 @@ import type { DependenciesComparator } from '../types';
* @param comparator function to validate dependency changes
* @returns useMemo result
*/
export const useCustomCompareMemo = <Factory extends () => unknown, Deps extends DependencyList>(
factory: Factory,
export const useCustomCompareMemo = <T, Deps extends DependencyList>(
factory: () => T,
deps: Deps,
comparator: DependenciesComparator<Deps>
) => {
): T => {
const dependencies = useRef<Deps>();

if (dependencies.current === undefined || !comparator(dependencies.current, deps)) {
dependencies.current = deps;
}

// eslint-disable-next-line react-hooks/exhaustive-deps -- missing factory function
return useMemo(factory, dependencies.current);
return useMemo<T>(factory, dependencies.current);
};

0 comments on commit 99a7c9e

Please sign in to comment.