Skip to content

Commit

Permalink
fix: useUpdate hitting maxInt, failing to trigger rerender
Browse files Browse the repository at this point in the history
Signed-off-by: Olzhas Alexandrov <olzhas@alexandrov.co>
  • Loading branch information
o-alexandrov committed Dec 5, 2019
1 parent 11aad69 commit 93e7291
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/useUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { useCallback, useState } from 'react';

const incrementParameter = (num: number): number => ++num;
/**
* MIN & MAX safe integers are literals due to no support in IE
*/
const minInt = -1000000000
const maxInt = 9007199254740991
const incrementParameter = (num: number): number => {
return num !== maxInt ? (num += 1) : minInt;
}

const useUpdate = () => {
const [, setState] = useState(0);
const [, setState] = useState(minInt);
// useCallback with empty deps as we only want to define updateCb once
return useCallback(() => setState(incrementParameter), []);
};
Expand Down

0 comments on commit 93e7291

Please sign in to comment.