A place to cache linked articles (think custom and personal wayback machine)
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

index.md 12KB

il y a 4 ans
1234567891011121314151617181920212223242526272829303132333435363738394041
  1. title: React Is A Terrible Idea
  2. url: https://www.pandastrike.com/posts/20150311-react-bad-idea
  3. hash_url: 707f575e5236f920d360f65119518c74
  4. <p>There&#39;s been a lot of excitement lately about React. React seems to have bumped Angular from the top of <a href="http://is.gd/wuZDbK">the hippest-framework mountain</a>. This is unfortunate, because both of these frameworks are <a href="https://news.ycombinator.com/item?id=9117028">bad for your application&#39;s health</a>. They&#39;re also bad for the entire software industry. For new applications, please, for the love of all things open, use <a href="http://webcomponents.org/">Web components</a> instead.</p>
  5. <p>React&#39;s design is bad. I could give you a lot of specifics—separation of concerns, coupling views with models, the focus on needless optimizations, the importance of supporting open standards—but I&#39;m going to tell you a story, instead. The story is about a startup with a popular product and lots of venture capital. This startup decided that they wanted to make their Web app extra special. Like many who came before them, they decided that HTML and CSS just weren&#39;t <em>quite</em> good enough for them.</p>
  6. <h2>The Wayward Startup</h2><p>They went to a great deal of trouble to build their own rendering engine, using Canvas. And it worked! Their Web app was now extra special. However, as it turned out, the browser vendors were working on solving the very problems that had motivated this startup to write their own rendering engine. In fact, just two weeks after the startup announced their extra special rendering engine, a major browser release included a solution to one of the biggest problems of all.</p>
  7. <p>The startup is <a href="https://flipboard.com/">Flipboard</a>. The <a href="http://engineering.flipboard.com/2015/02/mobile-web/">major problem is smooth scrolling</a>, which <a href="https://www.mozilla.org/en-US/firefox/36.0/releasenotes/">just dropped in Firefox</a>. The framework they wrote is <a href="https://github.com/flipboard/react-canvas">React-Canvas</a>, which was largely motivated by the desire for smooth scrolling. Instead of contributing to various efforts to speed DOM rendering, Flipboard decided to write their own canvas-based rendering engine. Was it cool? Yes. Was it impressive? Yes. But was it wisely constructed? No. Instead of a solution everyone can benefit from, we have a solution you can only benefit from if you use React. Even if React was a good design (it&#39;s not), that&#39;s still severely limiting the potential audience.</p>
  8. <p>Panda Strike is not a mindless cult, of course. Some of us think it&#39;s silly to support 60 FPS animations so your users can look at magazines, aka static content. Some of us buy <a href="http://daringfireball.net/2015/02/60_frames_per_second_and_the_web">John Gruber&#39;s argument that iOS-level responsiveness is not really optional any more</a>. But we all agree the engineers at Flipboard are obviously very talented, and React Canvas might be useful to someone. And that it would have been better as a simple Canvas library, without being tightly coupled to React.</p>
  9. <h2>Every Innovation Does Not Need Its Own Framework</h2><p>Internally, React depends on virtual DOM. The advantage over browser DOM is that virtual DOM nodes are relatively lightweight. You can create thousands of virtual DOM nodes without any performance impact on your application. Not only that, but you can diff virtual DOM trees and get a patch that can be applied to the actual browser DOM. That seems like it might, indeed, be handy. But what has that got to do with reactive programming? Or embedding markup in JavaScript?</p>
  10. <p>Nothing. As proof, I submit to you this <a href="https://github.com/Matt-Esch/virtual-dom">nice, stand-alone virtual DOM implementation</a>. Here&#39;s what the authors said about <a href="https://gist.github.com/Raynos/8414846">why they wrote a stand-alone version</a>:</p>
  11. <blockquote>
  12. <p>One important part of the virtual DOM approach is that it is a
  13. <strong>module</strong> and it <strong>should do one thing well</strong>. The virtual DOM
  14. is only concerned with representing the virtual DOM. The <code>diff</code>
  15. <code>batch</code> and <code>patch</code> functions are only concerned with the
  16. relevant algorithms for the virtual dom.</p>
  17. <p>The virtual DOM has nothing to do with events or representing
  18. application state. The below example demonstrates the usage
  19. of state with <code>observ</code> and events with <code>dom-delegator</code>. It
  20. could just as well have used <code>knockout</code> or <code>backbone</code> for state
  21. and used <code>jQuery</code> or <code>component/events</code> for events.</p>
  22. </blockquote>
  23. <p>The same things could be said of a Canvas-based rendering engine.</p>
  24. <h2>The Real Purpose of React</h2><p>Even if Flipboard spent <a href="https://www.crunchbase.com/organization/flipboard">all their venture capital</a> on improving the rendering engines in browsers, they couldn&#39;t spend nearly as much as Facebook is capable of. Whatever sins of misplaced enthusiasm Flipboard may have committed, Facebook has committed tenfold with React. What if, instead of promoting an open source framework that only benefits applications that adopt it, they instead put their efforts into making open source browsers better?</p>
  25. <p>They won&#39;t do this, though, because it isn&#39;t in their interest to do so. You may recall <a href="http://techcrunch.com/2012/09/11/mark-zuckerberg-our-biggest-mistake-with-mobile-was-betting-too-much-on-html5/">Facebook&#39;s abrupt about-face on their mobile app</a> back in 2012, when Mark Zuckerberg said building on HTML5 was his company&#39;s &quot;biggest mistake.&quot; What you may not recall was <a href="http://www.sencha.com/blog/the-making-of-fastbook-an-html5-love-story/">Sencha took this claim as a challenge, and built a great HTML5 implementation of the same app</a>, effectively demonstrating that Facebook&#39;s criticisms of HTML5 were largely unfounded. Unlike Google, whose revenue largely depends on their search engine–which, in turn, depends on people using the Web for everything–Facebook has <a href="http://www.theverge.com/2015/1/4/7488495/facebook-is-the-new-aol">always benefited from being a walled garden</a>. Put another way, Facebook doesn&#39;t care if you use the Web, it only cares that you use Facebook.</p>
  26. <p>So why release an open source Web framework at all? Because <a href="http://www.thewire.com/technology/2014/07/inside-the-ugly-recruitment-battle-between-facebook-and-google/374386/">Facebook is battling Google for engineers</a>. So you&#39;ve got a big fight between two companies over which company is the coolest place to work, and both of them are companies that your grandparents love. How are you going to win this fight? One way is to have the hippest Web framework.</p>
  27. <p>Basically, both Google and Facebook are desperate to find a baseball cap
  28. that they can put on backwards. Angular is Google&#39;s baseball cap. React is
  29. Facebook&#39;s.</p>
  30. <p><img src="https://www.pandastrike.com/assets/images/web-surfin-time.gif"></p>
  31. <h2>The Open Web Is More Important Than You Think</h2><p>Meanwhile, Web components are now available in Chrome and Firefox, with polyfills for older browsers. Web components are the biggest leap forward for the Open Web in years…and, yet, we&#39;re talking about React, a completely unnecessary framework. (Seriously, why not just introduce JSX and virtual DOM as separate libraries? And, as far as the data flow stuff goes, that&#39;s not even a new thing. People have been doing that for years.) This is bad, because the Open Web is important, and ten years ago, we nearly lost it.</p>
  32. <p>Today, Microsoft looks like a lost wildebeest, wandering the Saharan plains of failed products. But ten years ago, they were poised to own the Web. If you&#39;ve been in this business less than ten years, you might not realize how close we were to a world where the mobile Web would have required using Windows Phone. Don&#39;t believe me? <a href="http://en.wikipedia.org/wiki/United_States_v._Microsoft_Corp.">Microsoft was actually ruled to be a monopoly</a> in the United States, and <a href="http://money.cnn.com/2013/03/06/technology/microsoft-eu-fine/">is still dealing with the aftermath</a>. To this day, we still hear about the need to support Internet Explorer 8, which is the new Internet Explorer 6, a useless, bug-ridden weight that tied down the entire industry for nearly a decade. And the reason we&#39;re largely free of that weight today is the emergence of Firefox and WebKit.</p>
  33. <p>That didn&#39;t happen by accident.</p>
  34. <h2>How You Can Help</h2><p>We have the luxury of complaining about “slow” rendering times in the DOM because we can take for granted a browser platform that is <a href="http://www.chromium.org/developers/calendar">improving faster than</a> <a href="https://hg.mozilla.org/releases/mozilla-release">we can even make use of the new features</a>. This includes <a href="http://www.chromium.org/developers/design-documents/gpu-accelerated-compositing-in-chrome">major rendering improvements</a>. We don&#39;t need more workarounds; what we need is more investment in making the platform we have, whose very existence is a near-miracle, better. And you don&#39;t have to know C or iOS internals to be a part of that. All you have to do is make use of the platform.</p>
  35. <p>That&#39;s right. Just by using new innovations like Web components, instead of work-arounds like React, you&#39;re contributing to the success of the Open Web. You&#39;re voting with your code. With adoption comes community, and with community comes an ecosystem. In two years, we could have a Tower of Babel of recruiting-campaigns-posing-as-frameworks, or we could have an <a href="http://component.kitchen/components">ecosystem of reusable Web components</a>.</p>
  36. <p>The best part is that you don&#39;t have to compromise to get the benefits of Web components. Because they&#39;re actually a much better abstraction for building human-computer interfaces than anything React or Angular have to offer. What a lot of people don&#39;t realize is that components were implicit in MVC <a href="http://heim.ifi.uio.no/~trygver/1979/mvc-2/1979-12-MVC.pdf">when it was first introduced</a>. They&#39;re also the basis of pretty much every other HCI platform ever, including iOS and Android. So it&#39;s not like we&#39;re talking about limiting ourselves by using Web components. Quite the contrary, Web components represent a far superior option.</p>
  37. <h2>React, Bad; Web Components, Good</h2><p>A good framework supports separation of concerns. Web components do not impose a way of representing views or attaching event handlers or rendering to the DOM. A bad framework couples those things together so that you can&#39;t use one without the others. That&#39;s what React does.</p>
  38. <p>Sure, you can use JSX if you want, but that&#39;s the worst part of React anyway. JSX wants you to couple your view with the model and controller. It&#39;s a bad idea. Don&#39;t do that. Virtual DOM is great, if you need it, but you probably don&#39;t. And it was <a href="https://github.com/Matt-Esch/virtual-dom">thankfully FTFYd anyway</a>. React is not a good framework. And Web components aren&#39;t even a framework really, so much as <strong><em>how browsers work now</em></strong>. More generally, components are how pretty everything but the Web has worked for decades.</p>
  39. <p>The solution to the problems on the Web is never, ever going to be to tightly couple presentation, behavior, and data. Your Web apps will ultimately become unmaintainable hair balls of unreadable code. The solutions will come in the form of better Web browsers, and reusable components that <em>reduce</em> coupling instead of increasing it. In other words, they&#39;ll be based on best practices that have survived decades of evolution in the software industry. In the meantime, your best bet is typically going to be to use what works and focus on the problems you can do something about, without trying to reinvent the browser from inside the browser.</p>