(评论)
(comments)

原始链接: https://news.ycombinator.com/item?id=43615624

Hacker News讨论总结: 文章“在React中 {Transitions} = F(state)”讨论了如何将React应用视为状态机的模型,其中UI是状态的函数 (UI = f(state))。然而,dfabulich认为,将复杂的应用正式地建模为状态机,特别是用图表表示,通常是不切实际的,且价值不大。状态机图在状态和转换数量超过一定程度后会变得笨拙且无用。React的优势在于将UI视为状态的纯函数,这比管理复杂的状态图更有效。这是一个更简单有效的模型。 dvtkrlbs补充道,在真实的React应用中,组件经常依赖于上下文和第三方资源,使得纯粹的UI = f(state)范式并不总是直接适用,尽管对于实际的视图组件来说,这是一个有益的方法。

相关文章
  • 在React中,{Transitions} = F(state)。 2025-04-07
  • (评论) 2024-08-18
  • (评论) 2025-03-13
  • (评论) 2025-03-20
  • (评论) 2025-03-17

  • 原文
    Hacker News new | past | comments | ask | show | jobs | submit login
    In React {Transitions} = F(state) (jordaneldredge.com)
    14 points by captbaritone 31 minutes ago | hide | past | favorite | 2 comments










    > A React application can be thought of as modeling a state machine. Each render takes a state and produces the UI for that state. This is the famous UI = f(state) mental model of React. But, for complex applications, formally enumerating a transition table is often not feasible. When the number of possible states is not finite, a table will not suffice and we must instead define a mapping: a conceptual function which takes in a state and returns the set of valid transitions for that state.

    Not really, though. While you _can_ model any program as a state machine, doing so typically adds nothing to your understanding of the program.

    State-machine diagrams are especially useless. A diagram is only useful if it has about a dozen things in it, maybe two dozen, maximum. But that doesn't mean you can model a dozen _states_ in a state-machine diagram; in these diagrams, the _transitions_, the arrows between the states, are at least as important as the states themselves. So the diagrams are useless when the number of states + the number of transitions between them are greater than a few dozen. Typically that means state diagrams are only with a handful of states with a few transitions hanging off of each.

    But, if your problem is simple enough that you can represent it with a handful of states with two or three transitions each, then you have a _very_ trivial problem, so trivial that the state-machine diagram likely added nothing at all to your understanding of it.

    This is why no UI frameworks actually _do_ model UI as a set of states with transitions. It's easier to model the _state diagram_ as a _function_ and then just forget about the state diagram.

    That's what React is all about! A pure function, UI = f(state). It's _better_ than a state diagram.

    This article is saying: "Hey, you could think of React, something conceptually simple, as something unnecessarily complicated, instead!" Gee, thanks?



    I dont think this is really true in practice. Most of the time there is context and 3rd party sources components get from. It is good idea to have your actual view use this paradigm though.






    Join us for AI Startup School this June 16-17 in San Francisco!


    Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact



    Search:
    联系我们 contact @ memedata.com