Skip to content

Commit

Permalink
feat: 🎸 improve useCounter interface
Browse files Browse the repository at this point in the history
BREAKING CHANGE: useCounter interface changed
  • Loading branch information
streamich committed Oct 29, 2018
1 parent 9a1e9ce commit 395e82b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
9 changes: 7 additions & 2 deletions src/__stories__/useCounter.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import {useCounter} from '..';
import ShowDocs from '../util/ShowDocs';

const Demo = () => {
const [value, inc, set] = useCounter();
const [value, {inc, dec, get, set, reset}] = useCounter(5);

return (
<div>
<div>{value}</div>
<div>{value} is {get()}</div>
<button onClick={() => inc()}>Increment</button>
<button onClick={() => dec()}>Decrement</button>
<button onClick={() => inc(5)}>Increment (+5)</button>
<button onClick={() => dec(5)}>Decrement (-5)</button>
<button onClick={() => set(100)}>Set 100</button>
<button onClick={() => reset()}>Reset</button>
<button onClick={() => reset(25)}>Reset (25)</button>
</div>
);
};
Expand Down
31 changes: 25 additions & 6 deletions src/useCounter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
import {useState} from './react';
import useGetSet from './useGetSet';
import {useCallback} from './react';

export type Increment = (inc?: number) => void;
export type Set = (value: number) => void;
export interface CounterActions {
inc: (delta?: number) => void;
dec: (delta?: number) => void;
get: () => number;
set: (value: number) => void;
reset: (value?: number) => void;
}

const useCounter = (initialValue: number = 0): [number, Increment, Set] => {
const [value, set] = useState<number>(initialValue);
const useCounter = (initialValue: number = 0): [number, CounterActions] => {
const [get, set] = useGetSet<number>(initialValue);
const inc = useCallback((delta: number = 1) => set(get() + delta), []);
const dec = useCallback((delta: number = 1) => inc(-delta), []);
const reset = useCallback((value: number = initialValue) => {
initialValue = value;
set(value);
}, []);
const actions = {
inc,
dec,
get,
set,
reset,
};

return [value, (inc = 1) => set(value + inc), set];
return [get(), actions];
};

export default useCounter;
11 changes: 5 additions & 6 deletions src/useGetSet.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import {useRef} from './react';
import {useRef, useCallback} from './react';
import useUpdate from './useUpdate';

const useGetSet = <T>(initialValue: T): [() => T, (value: T) => void] => {
const update = useUpdate();
let state = useRef(initialValue);

const get = () => state.current;
const set = (value: T) => {
const update = useUpdate();
const get = useCallback(() => state.current, []);
const set = useCallback((value: T) => {
state.current = value;
update();
};
}, []);

return [get, set];
};
Expand Down

0 comments on commit 395e82b

Please sign in to comment.