|
|
|
|
|
by curryst
1949 days ago
|
|
It's weird, but count is basically a singleton here, where the state is shared with the thing that actually renders this. > Wouldn’t everything be a lot less magical if it was oop? Then you’d go ‘input_1.toggle_state’ and it is pretty clear to everyone which variable is being toggled. I think that's mostly mirroring the syntax from React (where things used to be classes, although functional React is getting more popular). The main advantage of this singleton based approach is that the dataflow is unidirectional. It doesn't show up in their simple examples, but in more complicated ones, those variables get passed to child components. If you do OOP, you're passing a mutable object to the child component. With this approach, they get a read-only copy of the variable. You can overwrite the value, but it won't propagate up to the parents, so the component can only screw up itself. It's different, but I don't think particularly magical. The magic is basically "for each component, see what singletons were passed to children. If any of the children's singletons changed, re-render the affected children". I'm not a React expert by any means, but once I got used to that functional paradigm, it doesn't bother me. |
|