我发现有限状态机可以作为在 React 应用程序中管理 UI 状态的替代方法。我认为它们非常棒,但我对应该如何使用它们有点困惑。
我应该为整个应用程序构建一个状态机还是在每个 route 构建小型机器?
最佳答案
基于this spectrum discussion我了解到可以用状态图代替路由器。
例如,https://state-machine-yzgnuxxwmg.now.sh/
但是https://statecharts.github.io/how-to-use-statecharts.html的第一段建议在开始时在组件级别使用状态图。
When you first learn about statecharts, you might get the feeling that statecharts can be used to describe the entire behaviour of an application, all the way from which screens show as part of logging in, to the state of each checkbox and text field in every screen, all represented in a statechart. That would be a statechart from hell, and an even bigger maintenance burden. Instead, the focus should be to get a grip on the behaviour at the component level, whatever a component might be. A single screen would be a component, for sure. A single text field that might have some particular internal behaviour (e.g. it changes color based on various flags like required or invalid) might warrant that to be wrapped in a component with a statechart to describe its behaviour.
关于javascript - 在React中,有限状态机是否取代了路由器的角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48677597/