Skip to content

Commit

Permalink
feat: use useReducer in useUpdate hook, instead of useState + useCall…
Browse files Browse the repository at this point in the history
…back
  • Loading branch information
streamich committed Jan 12, 2020
2 parents 477c164 + baab9df commit 6575b14
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/useUpdate.ts
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;

0 comments on commit 6575b14

Please sign in to comment.