javascript - 在React中,有限状态机是否取代了路由器的角色?

标签 javascript reactjs state finite-automata state-machine

我发现有限状态机可以作为在 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/

相关文章:

php - Flickr API : Get list of all images with specific sizes

javascript - MobileFirst Cordova 升级

javascript - 我如何从react中的redux状态中提取数据

javascript - 访问前检查数据是否存在,防止访问未定义错误

javascript - 如何使用切换来对状态使用react?

javascript - 查找 Farbtastic 颜色选择器当前值

javascript - 如何更改页面中的所有链接?

javascript - Jest 测试无法解析 React 组件中的纯 HTML 标签

c# - 有限状态机和封闭

reactjs - 编辑数组后在 React 中重新渲染子组件数组