Skip to content

Commit

Permalink
fix(useSetState): memoize setState callback
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich authored Aug 23, 2019
2 parents ff07a6b + c60ee96 commit 0275329
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
18 changes: 18 additions & 0 deletions src/__tests__/useSetState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,21 @@ it('should merge changes into current state when providing function', () => {

expect(result.current[0]).toEqual({ foo: 'bar', count: 2, someBool: true });
});

/**
* Enforces cases where a hook can safely depend on the callback without
* causing an endless rerender cycle: useEffect(() => setState({ data }), [setState]);
*/
it('should return a memoized setState callback', () => {
const { result, rerender } = setUp({ ok: false });
const [, setState1] = result.current;

act(() => {
setState1({ ok: true });
});
rerender();

const [, setState2] = result.current;

expect(setState1).toBe(setState2);
});
11 changes: 7 additions & 4 deletions src/useSetState.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { useState } from 'react';
import { useState, useCallback } from 'react';

const useSetState = <T extends object>(
initialState: T = {} as T
): [T, (patch: Partial<T> | ((prevState: T) => Partial<T>)) => void] => {
const [state, set] = useState<T>(initialState);
const setState = patch => {
set(prevState => Object.assign({}, prevState, patch instanceof Function ? patch(prevState) : patch));
};
const setState = useCallback(
patch => {
set(prevState => Object.assign({}, prevState, patch instanceof Function ? patch(prevState) : patch));
},
[set]
);

return [state, setState];
};
Expand Down

0 comments on commit 0275329

Please sign in to comment.