from Hacker News

Khan Academy's React style guide

by mdp on 10/21/15, 7:39 AM with 74 comments

  • by unoti on 10/21/15, 3:58 PM

    I've been working on learning React, and finding it particularly difficult. It appears there are a large number of things I need to have in place and pieces of knowledge I need before I can use it. These include some kind of Js compilation step like Webpack or Browserfy, something like Babel, a knowledge of how to use ES6, an understanding of React, and an understanding of how to use React-Router.

    Although I've done some Javascript on the front end, I haven't done the other things I mentioned. The tutorials all seem to assume I know how to do everything but one little piece of detail, and I'm finding it difficult to bite on the elephant. It's hard to tell where to start on learning this stuff, and how much I need to learn before I can use it.

    Any suggestions for what resources and approach to use to learn react? My goal eventually is an app that runs in 3 versions: web, iOS, android. I don't intend to use javascript on the server.

  • by andreasklinger on 10/21/15, 2:15 PM

    Request for comment:

    Linters are by far powerful enough by now.

    Can we (as community) switch to documenting style guides as linter rulesets + custom linters. Eg for javascript: eslint and jscs

    Written style guides are good for understanding why - but linters actually help others to adapt to it quicker

  • by iamjs on 10/21/15, 2:58 PM

    It's probably a good time to start looking at using the Fetch API [1] for making AJAX requests instead of using jQuery or Backbone (or even XMLHttpRequest). Support seems to be growing quickly and Github's polyfill [2] can help cover the gaps.

    [1] https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API [2] https://github.com/github/fetch

  • by tzury on 10/21/15, 11:23 AM

    John Resig (jQuery creator) is one of the chief architects at there, and yet you see, this line:

        Never use jQuery for DOM manipulation
    
    Nice.
  • by pramodliv1 on 10/21/15, 12:14 PM

    A couple of questions:

    >Do not use Backbone models

    I use Backbone models for ajax since it makes decisions such as PUT vs POST and model.save() looks cleaner than $.ajax. Also, Backbone collections provide a declarative way to handle sorting and duplicate models. But these models are internal to the Store and not exposed to the views. I'm still a React newbie. Is this a valid reason to continue using Backbone?

    2. It seems as though Khan Academy do not use React for SVG elements in their interactive exercises. For example, https://www.khanacademy.org/math/geometry/transformations/hs... Do you plan to migrate SVG to React?

  • by cnp on 10/21/15, 6:16 PM

    I'm curious why the top is preferred over the bottom:

    Open elements on the same line.

    The 80-character line limit is a bit tight, so we opt to conserve the extra 4.

    Yes:

      return <div>
        ...
      </div>;
    
    No:

      // "div" is not on the same line as "return"
      return ( 
        <div>
          ...
        </div>
      );
    
    The latter I feel is much more readable and clear than the former, as it has a certain symmetry and cohesion that you find in normal html. The top one seems to be break readability. Wondering if anyone can comment on this specific decision?
  • by traviswingo on 10/21/15, 3:08 PM

    I find it funny seeing this section:

    https://github.com/Khan/style-guides/blob/master/style/react...

    Even though John Resig is one of their main devs.

  • by aaronkrolik on 10/21/15, 2:08 PM

    Style question re dom manipulation: third party embeds, such as twitter, instagram often come as specially classed blockquote elements that are swapped with iframes by a jquery plugin. What is the best way to integrate this with react?
  • by codingdave on 10/21/15, 12:25 PM

    "Fit them all on the same line if you can." (HTML Properties) -- OK, easy enough, my editor has no limit on its line length. I can always fit them on one line. Or did they expect it to fit on one line visually. If so, at what width to they expect my editor window to be?

    In all seriousness, though, I appreciate the brevity of this guide. It can be quickly read and understood, and is not the fully-fledged book I've seen from other places.

  • by liquidise on 10/21/15, 3:18 PM

    A number of these guidelines reinforce my biggest complaint with React: it is architecturally difficult to avoid monolithic view files.

    In a traditional web app, we have 4 layers: client views, client app, server app, database. React, described as a strict view layer, in reality is being used as much more. At this point, it is not just consuming the client app, but is also taking nibbles at the server app as well.

    To each their own of course, but i would ask people to hesitate about these decisions. The architectural issues with monolithic views is well known, and just because we have a shiny new tool does not mean we should throw that understanding by the wayside.

    Source: i work full-time on a React and Backbone app

  • by twsted on 10/21/15, 12:27 PM

    "Do not use Backbone models."

    This seems a little strong. What is the reason for this guideline? I know of many projects that are combining the use of React with Backbone.

  • by amelius on 10/21/15, 11:23 AM

    It makes me feel so sad that the "state of the art" in front-end development is apparently rerunning your render code on every little update. Yes, I know there are shortcuts to making this more efficient, but in essence the technique remains inelegant in the sense that it does not extend well to other parts of the code (that runs computations that might also partially, and not fully, change on an update).
  • by lalwanivikas on 10/21/15, 2:07 PM

    React and Khan Academy reminded me of this funny tweet by one of their developers: https://twitter.com/jdan/status/655432901782302720
  • by NoCulturalFit on 10/21/15, 1:29 PM

    I would love to read more about styling inline and completely remove external CSS files.

    Are there any CSS-frameworks that have been converted to JS but not are not their own components yet? It's easy to find React-Bootstrap but that comes with ready made components, I am looking for styling that's purely in JS so I can make my own components.

    Also would a route-component be considered logic or presentation, or maybe it is its own thing and they forgot to mention it?