Skip to content

Commit

Permalink
fix: make useRerender operate integer increment instead of bool swi…
Browse files Browse the repository at this point in the history
…tch (#711)

fix: make `useRerender` operate integer increment instead of bool switch

Fix #691
  • Loading branch information
xobotyi authored Apr 10, 2022
1 parent f95b6e3 commit 409a21f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/useRerender/__tests__/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ describe('useRerender', () => {
});

expect(result.current[1]).toBe(1);

act(() => {
// https://github.com/react-hookz/web/issues/691
result.current[0]();
result.current[0]();
});
expect(result.current[1]).toBe(2);

act(() => {
result.current[0]();
result.current[0]();
result.current[0]();
});
expect(result.current[1]).toBe(3);
});
Expand Down
4 changes: 2 additions & 2 deletions src/useRerender/useRerender.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useCallback } from 'react';
import { useSafeState } from '..';

const stateChanger = (state: boolean) => !state;
const stateChanger = (state: number) => state + (1 % Number.MAX_SAFE_INTEGER);

/**
* Return callback function that re-renders component.
*/
export function useRerender(): () => void {
const [, setState] = useSafeState(false);
const [, setState] = useSafeState(0);

return useCallback(() => {
setState(stateChanger);
Expand Down

0 comments on commit 409a21f

Please sign in to comment.