Skip to content

Commit

Permalink
Merge pull request #50 from sophiebits/patch-1
Browse files Browse the repository at this point in the history
useImmerReducer: only call produce() when reducer changes
  • Loading branch information
mweststrate committed Apr 6, 2020
2 parents 8dc5c91 + 751c448 commit b392f0d
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import produce, { Draft } from "immer";
import { useState, useReducer, useCallback } from "react";
import { useState, useReducer, useCallback, useMemo } from "react";

export type Reducer<S = any, A = any> = (
draftState: Draft<S>,
Expand All @@ -25,6 +25,6 @@ export function useImmerReducer<S = any, A = any>(
initialAction?: (initial: any) => S
): [S, React.Dispatch<A>];
export function useImmerReducer(reducer, initialState, initialAction) {
const cachedReducer = useCallback(produce(reducer), [reducer]);
const cachedReducer = useMemo(() => produce(reducer), [reducer]);
return useReducer(cachedReducer, initialState as any, initialAction);
}

0 comments on commit b392f0d

Please sign in to comment.