18 ms·
React: Another Level of Indirection
- daemonk 13y agoI wonder if it's possible for browsers to start implementing this natively. Have the browser make a shallow copy of the DOM and allow developers to manipulate the shallow copy. Then have a update() function that will take care finding the diffs and update the actual DOM. Seems like it would be much faster than a javascript implementation also.
- drcode 13y agoYes, this I'm hoping this will happen over the next few years... this is how us web developers are going to escape DOM manipulation hell.
- girvo 13y agoAt that pint you have now implemented a desktop UI styled API 'n the browser! I'm not against that by any means, I just think it's funny :)
- jt2190 13y agoThis is exactly the goal. The DOM's strength was representing documents, not user interfaces. In this era of web apps, trying to use the DOM to build UIs is a massive pain. The web is really years behind in providing a native UI tool kit, and all of this DOM abstraction is just an attempt to paper over the real problem: The DOM is not good for building UIs.
- crazygringo 13y ago> The DOM is not good for building UIs. Funny, after years of Win32 C++, VB, and then web development, I find I can slap together an interface so much faster and better-looking using HTML/CSS/JS than anything for the desktop. However, "slap together" may be the important phrase -- putting together a well-architected, consistent, extendable, etc. interface is perhaps a little hairier. But the DOM is great for building UI's quickly, with trivially easy custom styling and graceful resizing.
- jt2190 13y agoYou just build UI's using the DOM? No abstraction libraries like jQuery? You've just won my "real programmer" award.
- recuter 13y agoTechnically jQuery normalized the DOM between browsers and added some stuff like QuerySelectorAll that is now standard so you don't really necessary need it anymore for anything.
- rfnslyr 13y agoI don't even have a monitor. I just have a keyboard with two buttons, one for 0 and one for 1. My brain is the compiler. My level of assuming what my binary will do is on another levle. I'm the real programmer.
- jt2190 13y agoAh downvotes... I should know by now that nobody on the internet knows you're trying to make a joke unless you wrap your comment in <humor> tags. And to the downvoters: My apologies for the offending you. I was simply trying to point out that the grandparent post is not comparing apples to apples, since it's very likely that he's achieving "fast" web development because he's either developing trivial user interfaces (i.e. not using anything that would require a layout manager, not required to support older browsers, etc.) or because he's using lots of other technologies in addition to the DOM (third-party javascript libraries, like jQuery, for example.)
- drcode 13y agoYou're just seeing how bad C++ and VB are for UI work (by modern standards)
- clarkevans 13y agoI've been playing with Facebook's React to make an internal interactive resource scheduling system for our company. So far I think I'm getting it... and am thrilled with how it works. React eschews the attempt to split the View / ModelView that are the hallmark of most full-service frameworks. Instead of this separation, React applications are built by defining reusable, nested components, each having a render() method that mixes binding and logic to build sub-parts of a web page using their custom DOM. Each component has an internal mutable state and a set of read-only properties it gets from its parent context. In React, "data binding" is just part of rendering. Since React owns the DOM, it's incompatible with JQuery; or rather, if you use JQuery, you could do it in a leaf node and manage the boundary carefully. React tracks things by using a hierarchical identifier, each key unique within each level of the structure. It then has a predictable/fast algorithem for detecting changes and updating the DOM. Unless you provide your own component keys (used to build the DOM id) all the way down, you need to use CSS classes (rather than IDs). Finally, I'd say, React really seems like a library -- and one that has a low API to functionality ratio. That is, it really does something substantial with relatively few things you need to learn. If you have your own in-house framework, it might be something you could weave in gradually rather than do a wholesale adoption of a larger (measured via API surface) framework.
- Pacabel 13y agoI haven't used React, but the approach you describe sounds very much like how early C++ UI toolkits worked. Applications would be built of "reusable, nested components", often called "widgets". The widgets would be placed into a hierarchy, with widgets often creating their ancestors. Widgets would have a paint() method that would take the widget's internal state and the state of the widget's container(s) into account when displaying (or updating a previous display) of the widget. - Pacabel
- pygy_ 13y agoJust to let you know: the guidelines ask not to sign your (otherwise perfectly good) comments. http://ycombinator.com/newsguidelines.html http://ycombinator.com/newsguidelines.html
- xixixao 13y agoFor a quick try of React in CoffeeScript, I put together https://github.com/xixixao/mimosa-hyper https://github.com/xixixao/mimosa-hyper
- bsaul 13y agoI've tried to understand what´s so special with react from the project's home examples, but it seems to me like an api similar to angularjs's directives, only using javascript to build the widget´s html instead of a template. Could anyone here with an experience of the two enlighten me? Edit : to be more precise, i've written fairly complex directives in angular and i don't understand the problem react is better solving. Synchronisation between dom and model is something you have to deal with manually anyway since it often includes validations rules and default behaviors, that can't be guessed easily ( invalid user input, no value appearance, etc..)
- spicyj 13y agoDoes this post answer any of your questions? http://www.quora.com/Pete-Hunt/Posts/Facebooks-React-vs-AngularJS-A-Closer-Look http://www.quora.com/Pete-Hunt/Posts/Facebooks-React-vs-Angu... Declarative syntaxes are easier to work with because you can look at your template and see exactly what the UI will look like. Since you're not doing the DOM manipulation yourself, it's completely airtight and you don't need to worry about writing custom DOM manipulation code for each individual part of the UI that can change. Of course, in any framework you can rerender the entire DOM by setting innerHTML on the top level but that tends to be slow and you need to watch out about resetting scroll position, input focus (and value), etc. React takes care of all of this for you. (You're right that invalid user input is something you need to deal with regardless, but you should need to deal with that only when making forms, never when displaying data that you already have (which is the common case). As for fallback values, it's easy to write something like <div>{this.props.author || "(anonymous)"}</div> in React when building a view.)
- adamnemecek 13y agoThere was a quora thread where they did some superficial comparison http://www.quora.com/Pete-Hunt/Posts/Facebooks-React-vs-AngularJS-A-Closer-Look?share=1 http://www.quora.com/Pete-Hunt/Posts/Facebooks-React-vs-Angu...
- grayrest 13y agoIf you're already using directives to get all your app's state out of the DOM then there's not a lot of practical benefit to using React over Angular. You get some perf gains due to the DOM update batching and it's conceptually cleaner. I think Angular is better for a mixed designer/developer team if the dev builds directives with the goal of letting the designer work independently since non-directive Angular is extremely easy to learn. The excitement here is that React is a MUCH better match for Clojurescript. The React model is a one way rendering into a virtual DOM, computing a minimal edit list against the real DOM, and applying it in one batch. The idempotent render lines up with pure functions. The batch nature lines up with the epochal time model of Clojure's vars. Clojure's persistent data structures allow react to bypass model mutation checks. I discussed the topic of client side state with various people at a couple NYC Clojure meetups in the spring and at Clojure/West and everybody had ideas for a solution but nobody had THE solution. The React model, if not React itself, is that solution for Clojurescript. I'm disappointed that I didn't look into the implementation details of React when I checked it out this Summer and found it unremarkable.
- craigching 13y agoIn David Nolen's blog post on Om, he has a link to an Om version of TodoMVC. Does anyone see it at TodoMVC? I can't seem to find it. EDIT: Of course I find it just after I posted this! The link looks like a single link but it's actually two! :p
- feifan 13y agoI got started with React on a project last week. It's magic when it works, but initially it takes some thinking to figure out how all the components should fit together, and where the interactions should take place. It took me a while to figure out that my interactions on one component might trigger a method that should belong on a parent component a few levels up, and to have to pass that method down in the props for the child to call. React provides a syntax called JSX that looks a lot like regular HTML, but is transformed into native JS before it hits the React engine. It's brilliant, but requires node somewhere along the line to do the transformation. I got stung by this — building a Rails app, and Rails on Heroku ships with an ancient version of Node that causes the transformer to error out. Solution in the works (https://github.com/heroku/heroku-buildpack-ruby/pull/177 https://github.com/heroku/heroku-buildpack-ruby/pull/177), but if you're using Rails and Heroku, stay away from JSX for now
- acjohnson55 13y agoIn a Backbone project I'm working on, every app component and view passes a context object down to any children it creates, which is a copy of the context object it received upon creation, optionally augmented with methods it contributes from itself and other providers. So it's basically a component tree, with an API that grows as you descend down the branches (yet remains distinct between branches). In essence, the context forms a sort of scope that subcomponents can use to communicate with others. By convention, components "import" API calls into themselves in their constructors. This makes it very clear what the dependencies are. The beauty of this setup is that intermediary objects don't have to worry about explicitly mediating the interface between their ancestors and descendants. The only components that have to do any mediation are common ancestors of components that need to communicate across branches, but this mediation can be contained, rather than polluting the entire hierarchy. It's also trivial to mock out the context methods during testing. Similarly, you can use a sort of name-based polymorphism to provide alternate implementations of the context methods, dependent on configuration or parent component, perhaps. It seems to me that a similar scheme would fit nicely with React's model.
- ttty 13y agoAfter 1-2 hour of playing I've converted my jquery view (still not 100% same, but almost) to a react, look here. There is a mini tutorial too. https://gist.github.com/totty90/8209324 https://gist.github.com/totty90/8209324
- jules 13y agoAm I the only one who doesn't like React? It is an ad hoc hacky solution to the view update problem. Diffing DOM trees is not just done for efficiency, but also for correctness. If you have a textbox somewhere with the cursor in it, and you update some other part of the DOM because of a network event, you want to retain that cursor position. If you just blindly put a whole new DOM on the page then the user would be confused because the focus on the textbox and the cursor position would be lost. With DOM diffing you have to keep in mind how exactly the diffing is performed because it will have actual visible effects on the user interface when user interface elements have state that is not represented in the model. You just have to hope that Reacts diffing gives you the behavior that you want. And what about interface elements that have internal state but which are implemented in Javascript rather than in the browser itself? React doesn't do anything to solve the view update problem for them. Seems to me that we need a more principled solution that tackles the problem head on, instead of a hack that happens to do the right thing most of the time.
- spicyj 13y agoReact's diff algorithm is completely deterministic. For form inputs and other components which have their own state, it's recommended to put a `key` attribute on each one so that React understands what you meant if you reorder elements in a list: http://facebook.github.io/react/docs/multiple-components.html#dynamic-children http://facebook.github.io/react/docs/multiple-components.htm.... Idiomatic React code usually has very little state except at the top of the tree so this is rarely a problem.
- jules 13y agoRight, but why do diffing in the first place? First generating a whole new tree and then computing the diff is (1) wasteful (2) leads to problems because you need to find the correspondence between nodes to preserve state. If you do something like Self-Adjusting Computation [1], you can still write your program as if you are generating the DOM as a function of your data, but the machinery under the hood can compute the diffs directly by keeping track of the dependency graph and the correspondence between the old and the new is fully automatic. [1] http://www.umut-acar.org/self-adjusting-computation http://www.umut-acar.org/self-adjusting-computation Assume you have a function f that takes the model data as input and computes the DOM as output: dom = f(model) If we now do an update to some model data, that has some effect on the DOM. When executing f for the first time, the machinery of self-adjusting computation records the dependencies of the different dom elements on data in the model. If some part of the model is updated it can propagate the changes along those dependencies to compute the parts of the dom that change as a result.
- jchrisa 13y agoI spent the holidays building a four screen admin UI with lots of fast moving tables and lists, and React was able to work with me from who-cares spaghetti all the way to clean enough to hand-off to a team, all without missing a beat or imposing much up-front cost. I'm a fan now.
- porker 13y agoAs someone who spends his entire life writing admin screens and more user-friendly but complicated to develop variants of CRUD, I don't suppose you could release the source code of what you wrote?
- jchrisa 13y agoAlways do. I plan to debase this before merging to master (the history is a mess) but here it is https://github.com/couchbase/sync_gateway/tree/adminUI/utils/assets/jsx https://github.com/couchbase/sync_gateway/tree/adminUI/utils... (Edit to link to my jsx views.) Also I only barely used the forms stuff so I haven't got an opinion on it yet.
- porker 13y agoThanks. I've yet to find forms I love in anything, but not tried Reactive's take yet :)
- ChristianMarks 13y agoMy takeaway: Any other problems left? I can't think of any. That's something to discuss on Twitter. Meaning that the remaining problems are trivial misconceptions that can be disposed of within 140 characters.
- Edmond 13y agoLike this a lot! especially the live update example on the website. I think we are beginning to see how web developer tools that are browser-based make certain things possible that would never be possible with native tools.
- dkersten 13y agohow web developer tools that are browser-based make certain things possible that would never be possible with native tools What do you mean? Can you give me an example?
- Edmond 13y agoYes there are many...for instance visual layout driven web development is one obvious example. I know people who prefer native tools always get defensive about the idea of the browser becoming the default platform for web development (and possibly software development in general), but I really don't see it happening any other way. The ways browser tools for instance can use the browser itself to help simplify web development is something native tools are never going to be able to do. For instance I envision being able to integrate the debugging/developer facilities built into the browser directly into my browser-based developer product without much heavy lifting. I may be bias given that I am a founder of a startup (see profile for link) building a browser-based developer product...but I have also spent years working with native IDEs including attempting to build my own IDE (http://bit.ly/IwZCEL http://bit.ly/IwZCEL), the browser advantage is just apparent to me. Granted there are still improvements to be made to browsers in order to bring them to parity with native environments for software development in general but I think it is just a matter of time.
- dkersten 13y agoOh ok, I think I understand what you mean. Though working with QML in QtCreator has been a pretty streamlined and effective experience for me and for web development, at least right now (ie like you say, this will improve in time, in which case you may well be right) I see browser tools (eg the debugger) as a part of a much larger toolchain (editor, REPL, etc). Though you can build a native editor that has, for example, webkit embedded to augment what the tool can do. Or do something like LightTable which is, AFAIK, build on top of an embedded browser. I think a hybrid approach gives the best of both worlds. Then again, with the trend of everything moving into the browser, I guess this shouldn't be necessary in the future.
- ekanna 13y agoRactivejs http://www.ractivejs.org/ http://www.ractivejs.org/ is another good library which does all most all of these things in a smart way!
- chenglou 13y agoReact user here. Some comments seem to suggest that React doesn't play well with other libraries; it's really the opposite. Here's a React Chosen component in 25 lines of code: https://github.com/chenglou/react-chosen/blob/master/react-chosen.js https://github.com/chenglou/react-chosen/blob/master/react-c... The React todomvc examples also uses Backbone and/or Director.
- jgalt212 13y agoReclineJS is pretty awesome, albeit a bit slow, with larger data sets. I'd wonder how snappy it would be if it were ported over to react. http://okfnlabs.org/recline/ http://okfnlabs.org/recline/
- joesb 13y agoIt would be great if someone can extract the virtual DOM engine part into its own library. New framework/library with different approach in other area can be develop while utilizing the virtual DOM part. At least I would love to see the combination of Angular directive (ability to combine cross-cutting aspect into a single DOM node and, may be, digest loop) with React's virtual DOM.