Skip to content

Commit

Permalink
[FEATURE] Passing a function for draft or value for direct state usage (
Browse files Browse the repository at this point in the history
#76)

* Enables passing a direct value to the useCallback hook for using it as simple state

* Fixed typing error
  • Loading branch information
blackshot authored Mar 20, 2021
1 parent 7b19019 commit 324b0db
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ export type ImmerHook<S> = [S, Updater<S>];

export function useImmer<S = any>(
initialValue: S | (() => S)
): [S, (f: (draft: Draft<S>) => void | S) => void];
): [S, (f: ((draft: Draft<S> | S) => void) | S) => void];

export function useImmer(initialValue: any) {
const [val, updateValue] = useState(initialValue);
return [
val,
useCallback(updater => {
updateValue(produce(updater));
if(typeof updater === "function")
updateValue(produce(updater));
else
updateValue(updater);
}, [])
];
}
Expand Down

0 comments on commit 324b0db

Please sign in to comment.