React in theory

This post was originally published for Yellowme as React In Theory.
Some time ago, while I was reading Twitter as Vue was gaining popularity, the community asked Dan Abramov, "The React Guru" since React 16, whether he thought React as a library would ever be replaced by an alternative, pointing at the time to Vue.
He replied that it was possible something might replace React as a library in the future, but he trusted that React's model would prevail for much longer.
What is the React “model”?
The explanation here is based on a "formalized" description in the "react-basic" repository. It also explains that this is an attempt at a mental model of how the internal mechanism "works," but that in practice, as with everything, it is a little more complex.
Transformation
The central premise of React is that user interfaces are simply a projection of data into a "different shape." The same input gives the same output. A simple pure function.
I've read the quoted phrase from several authors, such as Erick Eliot and the React Core Team itself. We write functions all the time when developing software, functions that transform arguments into a "derivation" of those arguments as a response.
In React we can have:
function Greeting({ name }) {
return <p>{`Hello, ${name}`}</p>;
}
And this can be perfectly equivalent to:
function greeting(name) {
return `Hello, ${name}`;
}
React keeps the essence of programming in plain JavaScript code.
Abstraction
All software problems are solved with indirection. This is the ability to refer to data in a way other than by its value, and React applies this technique very transparently.
Returning to the example above, the Greeting function (component) lets us refer to it without thinking about whether it is actually a DOM element or a React element. From its name and arguments, we know it might present a greeting to the name supplied as an argument.
Composition
Composition is the essence of programming: taking a problem and dividing it into smaller parts, then building a complete solution by combining small solutions.
In my first high school programming class, I was told that software development is “the act of breaking a complex problem down into smaller problems, and composing simple solutions to form a complete solution to the complex problem.” — Erick Eliot
React gives us the ability to develop complex interfaces from small reusable UI elements.
function Header() {
// we assume that we have a function
// from which we get a user
// in our application
const user = useUser();
// without having to modify the behavior
// of Greeting we can use the same element
// in different parts of our application
return (
<div>
<Greeting name={user.name} />
<nav>
<a href="/profile">Profile</a>
</nav>
</div>
);
}
State
When I look at other libraries similar to React, the most interesting thing is always their "proposal" for handling application state.
UIs could be pure projections of data sent from any source to clients, but there is an intermediary we cannot control: the user. "State" could be the combination of the data in our application and user interactions.
Among the advantages is the ability to "travel through time" by knowing the state of the application at a given point. These patterns are strongly represented in libraries such as Redux.
A single function is responsible for combining user interactions and application data:
function countReducer(state, action) {
switch (action.type) {
case "increment": {
return { count: state.count + 1 };
}
case "decrement": {
return { count: state.count - 1 };
}
default: {
throw new Error(`Unhandled action type: ${action.type}`);
}
}
}
We can describe the user interactions that implement the possible state transformations:
function increment() {
dispatch({ type: "increment" });
}
Beyond JavaScript
Along my learning journey, I've been able to see how libraries like SwiftUI and Flutter work. In my opinion, they fit well with the React "model."
It's clear that, regardless of what new libraries or technologies appear, many of them retain the same essence I discovered in React.
Finally, it's important to mention that the points described here aren't a new invention by the React team. As I've mentioned at several points, these are the essence of programming, and seeing them implemented in what I consider an elegant way is what always motivates me to keep exploring React as a technology.
Some external links I use for inspiration and reference: