Skip to content

Commit

Permalink
feat(useUpdate): improve memory usage - now single function instance …
Browse files Browse the repository at this point in the history
…to increment all counters;
  • Loading branch information
xobotyi committed Oct 30, 2019
1 parent 58ddea3 commit 0f02fd0
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/useUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useState, useCallback } from 'react';
import { useCallback, useState } from 'react';

const incrementParameter = (num: number): number => ++num;

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

export default useUpdate;

0 comments on commit 0f02fd0

Please sign in to comment.