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 e4ceb8a
Showing 1 changed file with 4 additions and 4 deletions.
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 e4ceb8a

Please sign in to comment.