A place to cache linked articles (think custom and personal wayback machine)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 4 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. title: ReactJS For Stupid People
  2. url: http://blog.andrewray.me/reactjs-for-stupid-people/
  3. hash_url: 352a1ada40f12fec1e986f17364605f6
  4. <p><strong>TL;DR</strong> I struggled for a long time trying to understand what React is and how it fits in the application architecture. This post is what I wish someone had told me.</p>
  5. <h2 id="whatisreact">What is React?</h2>
  6. <p>How does React compare to Angular, Ember, Backbone, et al? How do you handle data? How do you contact the server? What the heck is JSX? What is a "component"?</p>
  7. <p>Stop.</p>
  8. <p>Stop it right now.</p>
  9. <p><strong>React is ONLY THE VIEW LAYER.</strong></p>
  10. <p>React is often mentioned in the same breath as other Javascript frameworks, but "React vs Angular" doesn't make sense because they aren't directly comparable things. Angular is a complete framework (including a view layer), React is not. This is why React is so confusing to understand, it's emerging in an ecosystem of complete frameworks, but it's just the view.</p>
  11. <p>React gives you a template language and some function hooks to essentially render HTML. That's all React outputs, HTML. Your bundles of HTML / Javascript, called "components", are allowed things like storing their own internal state in memory (such as which tab is selected in a tab view), but in the end you just barf out HTML.</p>
  12. <p>You absolutely <strong>cannot build a fully functional dynamic application with React alone.</strong> We'll learn more about why below.</p>
  13. <h2 id="thegood">The Good</h2>
  14. <p>After working with React for a while, I've seen three very important benefits surface.</p>
  15. <p><strong>1. You can always tell how your component will render by looking at one source file.</strong></p>
  16. <p>This may be the most important benefit, even though it is not different from Angular templates. Let's use a real world implementation example.</p>
  17. <p>Say you have to update your site's header with the user's name upon log in. If you're not using a Javascript MVC framework, you might do this:</p>
  18. <pre><code class="lang-markup">&lt;header&gt;
  19. &lt;div class="name"&gt;&lt;/div&gt;
  20. &lt;/header&gt;
  21. </code></pre>
  22. <pre><code class="lang-javascript">$.post('/login', credentials, function( user ) {
  23. // Modify the DOM here
  24. $('header .name').show().text( user.name );
  25. });
  26. </code></pre>
  27. <p>I can tell you from experience that this code will ruin your life and your friends' lives. How do you debug the output? Who updated the header? Who else has access to the header HTML? Who holds the source of truth for the name being visible? This DOM manipulation is <strong>just as bad as a GOTO statement</strong> for reasoning about your program. </p>
  28. <p>Here's how you might do it in React:</p>
  29. <pre><code class="lang-javascript">render: function() {
  30. return &lt;header&gt;
  31. { this.state.name ? &lt;div&gt;this.state.name&lt;/div&gt; : null }
  32. &lt;/header&gt;;
  33. }
  34. </code></pre>
  35. <p>We can tell instantly how this component will render. <strong>If you know the state, you know the rendered output.</strong> You don't have to trace program flow. When working on complex applications, especially in teams, this is critically important.</p>
  36. <p><strong>2. Bundling Javascript and HTML into JSX makes components easily understandable.</strong></p>
  37. <p>The weird mix of HTML / Javascript soup above might make you cringe. We've been conditioned to not put raw Javascript in the DOM (like <code>onClick</code> handlers) since we were wee developers. You'll have to trust me, though; working with JSX components is <strong>really nice.</strong></p>
  38. <p>Traditionally you separate views (HTML) from functionality (Javascript). This leads to monolithic Javascript files containing all functionality for one "page", and you have to trace complex flow from JS &gt; HTML &gt; JS &gt; bad-news-sad-time.</p>
  39. <p>Tying functionality directly to markup and packaging it in a portable, self contained "component" will make you happier and less filthy in general. Your Javascript has intimate knowledge of your HTML, so mashing them together makes sense.</p>
  40. <p><strong>3. You can render React on the server.</strong></p>
  41. <p>If you're building a public facing site or app and you're following the render-it-all-on-the-client path, you've already done it wrong. Client-only rendering is why <a href="https://soundcloud.com">Soundcloud</a> feels so slow and why <a href="http://stackoverflow.com/">Stack Overflow</a> (purely server side rendering) feels so fast. You can <a href="https://www.npmjs.org/package/react-server-example">render React on the server</a>, and you should.</p>
  42. <p>Angular and others encourage you to do disgusting things like render your page with PhantomJS and serve that to search engine crawlers based on user agent, or <a href="https://prerender.io/pricing">pay actual cash money for that as a service</a>. Ugh.</p>
  43. <h2 id="thebad">The Bad</h2>
  44. <p>Don't forget that React is <strong>only the view.</strong></p>
  45. <p><strong>1. You DO NOT GET any of the following:</strong></p>
  46. <ul>
  47. <li>An event system (other than vanilla DOM events)</li>
  48. <li>Any AJAX capabilities whatsoever</li>
  49. <li>Any form of a data layer</li>
  50. <li>Promises</li>
  51. <li>Any application framework at all</li>
  52. <li>Any idea how implement the above</li>
  53. </ul>
  54. <p>React on its own <strong>is useless for the real world.</strong> Worse yet, as we'll see, this leads to everyone reinventing the wheel.</p>
  55. <p><strong>2. The documentation is not "accessible" nor "good."</strong> Again, this is a blog post for stupid people. Look at the first part of the sidebar on the <a href="http://facebook.github.io/react/docs">documentation page</a>:</p>
  56. <p><img src="/content/images/2014/Sep/Screen-Shot-2014-09-22-at-12-08-18-AM.png" alt="React documentation sidebar"/></p>
  57. <p>There are three separate, competing quickstart guides. I'm overwhelmed and I'm not even drunk. The sidebar below that is straight out of my nightmares, with sections that obviously shouldn't be there, like "More About Refs" and "PureRenderMixin".</p>
  58. <p><strong>3. React is large for how little you get, including how little cross browser support.</strong></p>
  59. <p><em>Update:</em> React is not 144 KB as I <a href="/content/images/2014/Sep/Screen-Shot-2014-09-22-at-3-48-19-PM.png#full-img">previously wrote</a>. React is about <strong>35 KB gzipped</strong> over the wire.</p>
  60. <p><img src="/content/images/2014/Oct/react-size.png" alt="React file size"/></p>
  61. <p>That's <strong>without the <a href="http://facebook.github.io/react/downloads.html">react-with-addons</a> library</strong> you will need to actually develop a real app!</p>
  62. <p>That's <strong>without the <a href="https://github.com/es-shims/es5-shim">ES5 Shim</a> library</strong> you need to support IE8!</p>
  63. <p>That's <strong>without</strong> any sort of application library of any kind!</p>
  64. <p>React is a <strong>comparable size with Angular,</strong> even though Angular is a complete application framework. React is frankly bloated for how little functionality you get. Hopefully this will improve in the future.</p>
  65. <h2 id="stopsayingflux">Stop Saying Flux</h2>
  66. <p>Perhaps the most annoying part of React development is "Flux." It's far more confusing than React. The name "Flux" is a pretentious barrier to understanding.</p>
  67. <p><strong>There is no such thing as Flux.</strong> Flux is a concept, not a library. Well, there <a href="https://github.com/facebook/flux">is a library</a>, sort of:</p>
  68. <blockquote>
  69. <p>"Flux is more of a pattern than a framework"</p>
  70. </blockquote>
  71. <p>Ugh. The worst part is the name: React didn't reinvent the last 40 years of UI architecture knowledge and come up with some brand new concept for data management.</p>
  72. <p><strong>The concept "Flux" is simply</strong> that your view triggers an event (say, after user types a name in a text field), that event updates a model, then the model triggers an event, and the view responds to that model's event by re-rendering with the latest data. <strong>That's it.</strong></p>
  73. <p>This one way data flow / decoupled observer pattern is designed to guarantee that your source of truth always stays in your stores / models. It's a Good Thing™.</p>
  74. <p><strong>The bad side of Flux</strong> is that everyone is re-inventing it. Since there's no agreed on event library, model layer, AJAX layer, or anything, <a href="https://www.npmjs.org/package/compose-flux-dispatcher">there</a> <a href="https://www.npmjs.org/package/flux-action">are</a> <a href="https://www.npmjs.org/package/react-flux">many</a> <a href="https://www.npmjs.org/package/fluxxor">different</a> "<a href="https://www.npmjs.org/package/reflux">Flux</a>" implementations, and they all compete with each other.</p>
  75. <h2 id="shouldiusereact">Should I Use React?</h2>
  76. <p>Short answer: <strong>yes.</strong></p>
  77. <p>Long answer: unfortunately, yes, for most things.</p>
  78. <p><strong>Here's why</strong> you should use React:</p>
  79. <ul>
  80. <li>Works great for teams, strongly enforcing UI and workflow patterns</li>
  81. <li>UI code is readable and maintainable</li>
  82. <li>Componentized UI is the future of web development, and you need to start doing it now.</li>
  83. </ul>
  84. <p><strong>Here's why you should think twice</strong> before you switch:</p>
  85. <ul>
  86. <li>React will <strong>slow you down tremendously</strong> at the start. Understanding how props, state, and component communication works is not straightforward, and the docs are a maze of information. This will be countered, in theory, by a grand speed up when your whole team is on board.</li>
  87. <li>React does not support any browser below IE8, and never will</li>
  88. <li>If your application / website doesn't have very much dynamic page updating, you will be implementing a lot of code for a very small benefit.</li>
  89. <li>You will reinvent a lot of wheels. React is young, and because there's no canonical way to do events / component communication, you'll have to build large component libraries from scratch. Does your application have dropdowns, resizable windows, or lightboxes? You'll probably have to write those all from scratch.</li>
  90. </ul>
  91. <h2 id="thatsit">That's It!</h2>
  92. <p>Check out the follow-up post, <a href="http://blog.andrewray.me/flux-for-stupid-people/">Flux For Stupid People</a>.</p>
  93. <p>I hope this helped someone as stupid as me understand React better. If this post made your life easier, consider following me <a href="https://twitter.com/andrewray">on Twitter</a></p>