matthewp / robot

🤖 A functional, immutable Finite State Machine library

Home Page:https://thisrobot.life

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use action in function component?

anatoly314 opened this issue · comments

Hi,
Thank you for this library, and I have a question.
If I define machine inside the React function component I get the error:

Maximum update depth exceeded

If I define machine outside the React function component, I can't use an action to create a side effect inside the component.

Here some demo which I have done: https://codesandbox.io/s/material-demo-forked-t82k0?file=/demo.js
I want that function callMeWhenSuccess will be called when we enter the success state. In real world scenario, I will use it as a callback to close the Dialog window.

I suppose you are saying that you need access to something inside of the component within your action. You can pass data into a machine through context. In this example I'm passing a callback: https://codesandbox.io/s/material-demo-forked-13iow?file=/demo.js