React components are wonderfully encapsulated. Each component is a blueprint for what a focused bit of markup should look like at any moment. They're reusable and can change their behavior depending on the context provided. Does that remind you of another programming paradigm?
Let's talk about JavaScript. JavaScript has a prototypical inheritance model. That means different objects can have a common structure. The structure of one object can be derived from the structure of another.
It also means that changes to the original object are inherited in all derivative objects. Let me illustrate this with some code:
I begin by creating a function called Page
, which requires a content
parameter. A simple render
method returns that content, wrapped in a div
tag. This seems like a good starting point for a website.
Next, I decide to make a second type called Post
. Objects of this type have tags, so I create a new initialization function to store them. I want Post
to behave almost like a Page
type, so I call the Page
initialization function.
To inherit the Page
methods in Post
, I need to link their prototypes. I can then choose to override the render
method and add new methods to the derived type. I can also change the Page
type and these changes will be inherited by objects of the Post
type. The connection happens because a prototype is a reference and not a copy.
Depending on the programming languages you grew up with, prototypical inheritance might be tricky at first. Many new developers learn (incorrectly) that object-oriented code means class-oriented code. Dynamic concepts such as prototypes are foreign to them. In the past, this led to a few libraries implementing "pretend" classes. They created patterns that would make code appear as if it was class-oriented.
Then, ES6 added the class
keyword. It's a formalization of the pattern I just showed you. It's a syntactic shortcut to prototypical inheritance.
We could reduce the previous code to:
Note
If you're trying to run this using Node (preferably a version greater than 4.1), you may need to add use strict
at the top of the file.
Notice how much clearer things are? If you want to use classes, then this syntactic shortcut is brilliant!
Let's look at a typical ES5-compatible React component:
You've probably seen this kind of code before. It's called JSX and it's a JavaScript superset language. The idea is that the markup and the supporting logic are created and stored together.
Note
React components must return a single React node, which is why we wrap the tags and page elements in a div
element. If you are using React in the browser, you also need to render your components to an existing DOM node (like I've just rendered the post to .react
).
We'll get into some of the specifics in later chapters, but this is doing pretty much the same thing as before. We create a base component called Page
. It renders a property instead of a constructor parameter.
The Post
component composes the Page
component. This style of React code doesn't support component inheritance. For that, we need ES6 code:
We could still compose Page
within Post
, but that's not the only option with ES6. This code resembles the non-React version we saw earlier.
In upcoming chapters, we'll learn many useful features of ES6 that'll allow us to create modern, expressive React components.
Babel is the cross-compilation tool we'll use to turn ES6 code into ES5 code: