-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use useReducer in useUpdate hook, instead of useState + useCall…
…back
- Loading branch information
Showing
1 changed file
with
4 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
import { useCallback, useState } from 'react'; | ||
import { useReducer } from 'react'; | ||
|
||
const incrementParameter = (num: number): number => ++num % 1_000_000; | ||
const updateReducer = (num: number): number => (num + 1) % 1_000_000; | ||
|
||
const useUpdate = () => { | ||
const [, setState] = useState(0); | ||
// useCallback with empty deps as we only want to define updateCb once | ||
return useCallback(() => setState(incrementParameter), []); | ||
const [, update] = useReducer(updateReducer, 0); | ||
return update as (() => void); | ||
}; | ||
|
||
export default useUpdate; |