From 1d2cbe93037a71a04c1098a0fc4abbeffa15374f Mon Sep 17 00:00:00 2001 From: Beier Luo Date: Sun, 6 Jun 2021 09:42:03 +1000 Subject: [PATCH] fix: actions as dep issue --- src/stateMachine.tsx | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/stateMachine.tsx b/src/stateMachine.tsx index 7c4e15c..27424f0 100644 --- a/src/stateMachine.tsx +++ b/src/stateMachine.tsx @@ -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, + ), ); + + return { + actions: actionsRef.current, + state, + }; }