Skip to content

Instantly share code, notes, and snippets.

@basecode
Last active January 2, 2016 13:59
Show Gist options
  • Save basecode/8313779 to your computer and use it in GitHub Desktop.
Save basecode/8313779 to your computer and use it in GitHub Desktop.
7 things I learned from React sourcecode. Website: http://facebook.github.io/react

It's all about Components.

  • Components can be created at runtime via createClass
  • Stringified and parsed
  • Existing DOM can be parsed via a "mount" algorithm.

DOM changes

  • React is event based and not "frame" based. It's not using a timer. DOM changes are cached until all events are done and flushed at once.
  • setState triggers DOM changes explicitly

Dependency injection

  • Objects have injection property that holds setters for injecting dependencies
  • DOM is abstracted. Modules like performanceNow are using Date.now() fallback in case DOM (or performance.now) is not available

Transaction

Holds a task. A task that can be cached, pooled, queued and allows other transactions to wait until previous tasks are resolved. var transaction = perform(function() { doSth(); }); release(transaction);

WebWorker

There are hints that they tried but failed using WebWorkers (internally called "ReactWorker") so far. Apparently preventDefault was one of the problems they ran in to while running a Component within a separate thread. Maybe a future use case could be performing "Transactions" within a worker.

General

  • License: http://www.apache.org/licenses/LICENSE-2.0
  • Commonjs by default / supports amd too
  • Coding guidelines completely reasonable. Nothing fancy.
  • Compiler hinting, logging and assertions (including best practice suggestions) all over the place
    • if (__DEV__),
    • if ("production" !== "development") {
    • invariant(true, 'Must be true');
  • Some functions are "measured" at runtime. E.g. Time spend to mount a Component. Results are reported somehow. I guess mostely via console.
  • Function naming known from Cocoa
    • componentWillMount
    • componentDidMount
    • release

So much useful code

  • object pooling
  • services (performance.now())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment