Skip to content

Commit

Permalink
fix: actions as dep issue
Browse files Browse the repository at this point in the history
  • Loading branch information
bluebill1049 committed Jun 5, 2021
1 parent 1bc33c6 commit 1d2cbe9
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/stateMachine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,18 @@ export function useStateMachine<
state: GlobalState;
} {
const { state, setState } = useStateMachineContext();

return React.useMemo(
() => ({
actions: actions
? Object.entries(actions).reduce(
(previous, [key, callback]) =>
Object.assign({}, previous, {
[key]: actionTemplate(setState, callback),
}),
{},
)
: ({} as any),
state,
}),
[state, setState],
const actionsRef = React.useRef(
Object.entries(actions || {}).reduce(
(previous, [key, callback]) =>
Object.assign({}, previous, {
[key]: actionTemplate(setState, callback),
}),
{} as ActionsOutput<TCallback, TActions>,
),
);

return {
actions: actionsRef.current,
state,
};
}

0 comments on commit 1d2cbe9

Please sign in to comment.