Simpler UI Reasoning with Unidirectional Dataflow and Immutable Data
omniscientjs.github.io
omniscientjs.github.io
I've been doing experiments with Mithril (provides virtual DOM) and various functional reactive programming libraries (such as Kefir), and I think something like that is the right way to go.
Unless I've mis defined what "letting the component determine it needs to redraw" means in this context.
In time, React hopefully (and probably) gets support for stateless functions without the syntactic sugar (https://github.com/reactjs/react-future/blob/master/01%20-%2...). This will be supported when React adds support for functions returning VDOM elements.
Also, nuclear seems quite stable at this point, and the GitHub issues are about pretty practical stuff, while omniscient has had very recent breaking API changes and many of the issues are discussions about functional purity etc. Just my two cents on why I chose one over the other.
However, for the app I'm working on, I want most the single source of truth for most application state to be on the server. I'm passing data from server to client in JSON. Has anybody come up with a good way to serialize/deserialize immutable objects? I might try out transit-js, but I'm wondering if anybody has already gotten this working?
[0] http://fluxible.io/ [1] http://facebook.github.io/immutable-js/ [2] https://github.com/yahoo/fluxible-immutable-utils/
I guess I was initially hoping to use immutable data for the performance benefit; being able to implement a fast shouldComponentUpdate() with a simple equality check.
But I wonder how toJS() and fromJS() performance-wise... if they have to create new objects by deeply copying the objects, maybe that would be just as bad as doing no shouldComponentUpdate optimization.
I guess I'll just stick to the "premature optimization is the root of all evil" concept, and try to get something working with carefully mutable data first.
I have been experimenting with replacing REST style backend interaction with websockets an having the state manipulation round-tripping through the server.
Instead of setting values on mutable objects and telling the server about it postfact (having to deal with errors like ba validation and connectivity somehow), I would send a change command to the server, which would decide what to do, and push an updated state back. Once I detect this change of state, I update the UI.
This simplifies error handling a lot, since the user simply can't update state if an error occurs. And I get cuncurrent multi user editing for free.
Unless you want offline editing (which is hard), it's a big win.
With rest, it would be a much more manually implemented feature. The default would be to just fail silently and potentially drop important data on the next page load.
Edit: Actually when you pass all state as props, this will even apply to typing in a textarea. Better make sure the roundtrip is pretty short!
The update wouldn't be sent until it is unfocused or possibly after a set delay after the last keypress.
Furthermore, you can avoid unnecessary re-computation via the usual techniques like memoization. One of the big advantages of the "virtual DOM" is that we get to build documents using a real programming language, not some HTML templating system.
In that case, why isn't it just (more or less) equal in syntax to a function call?
Why do I need to build a graph, where a functional language would just let me build it implicitly.