React is a framework, not a library

Published on in Elsewhere and React

React is marketed as a view library, but it's more accurately a (view component) framework.

Table of contents

React is a framework because it's in control of when your code runs

A snippet from a GitHub gist by Peer Reynders (with minor grammatical edits by me):

The litmus test:

  • If your code calls it, it's a library.
  • If it calls your code, it's a framework.

Looking at the React tutorial's[1] Starter Code, ReactDOM.render() is the only part of React that your code calls. The rest, i.e. the majority of your code, is called by React.

Ipso facto[2]React is a framework because it is in control when most of your code runs.

There you have it. I have thought for some time that React is a framework, not a library, but I haven't had the words to explain why.

React vs jQuery

It becomes clearer from Peer's comparison of jQuery (a library) and React (a framework). Both are tools for dealing with the DOM, but they are used differently:

jQuery for the most part is used as a layer between your code and the browser. Your code calls jQuery to fulfill some objective – and when it's done, it's done. jQuery doesn't do anything until your code calls it the next time. Some jQuery code may run due to being referenced in an event handler or because of an async callback, but that is just in the nature of browser operations. jQuery as such isn't in control, your code is. jQuery is called a library because it's simply a collection of useful helpers; after you call each method, your code is firmly back in charge.

With ReactDOM.render() your code hands over control to React. Past that point React is in full control and your code only runs when React decides to run it. React isn't a layer that your code calls to deal with the nitty-gritty of the browser, but an environment that isolates your components from the browser and uses your components when it deems it necessary – which is exactly what a framework does.

[...]

So both jQuery and React stand between your code and the browser('s DOM), but your code's pattern of interaction with them is very different:

  • jQuery is a collection of helpers that your code calls whenever it needs to.
  • React on the other hand calls your code. [...]

Go read Peer's GitHub gist; it has more great points but isn't too long.

Does it matter?

Does it matter whether React is marketed and thought of as a framework or a library?

Should React embrace the fact that it's a framework? Would it affect React's ecosystem?

Good questions. What do you think? Send me an email (my address is on the home page) and let me know!

Footnotes

  1. Peer is referring to the React tutorial on reactjs.org.

  2. Wikipedia's definition of ipso facto:

    Ipso facto is a Latin phrase, directly translated as "by the fact itself," which means that a specific phenomenon is a direct consequence, a resultant effect, of the action in question, instead of being brought about by a previous action. It is a term of art used in philosophy, law, and science.