Learn The Web in about a week!
Learning should be fun and quick; no need to spend ages in just one single course.
What exactly is a React element?
Get to know about the most important concept in all of React — that of an element.
React is one of the most popular JavaScript UI libraries out there. In fact, it won't be wrong to say that it's the most popular one. As a frontend developer, knowing about the core concepts of React well enough is a preliminary.
In this respect, one concept that you must learn really well is that of an element in React. So what is an element? Well answering this question is the very goal of this entire article so let's get straight to reading.
The definition of a React element
Let's begin with the definition of a React element, quoting from the old docs:
React elements are the building blocks of React applications.
Indeed, an element represents one of the most fundamental concepts in React. A React app is simply nothing without elements. But this is a high-level, conceptual definition of an element, not a concrete definition. Let's see how that goes...
A React element is an abstraction over a DOM element or a component instance. For example, when you do the following in React
<h1>A heading</h1>you're basically creating an element which abstracts the underlying <h1> element in the DOM (specifically, an HTMLHeadingElement). Similarly, given a component, let's say Greeting, when you do the following:
<Greeting />you're creating an actual instance out of the component. In both cases, what you have is an element.
Also note that in both cases, the JSX code is merely calling the createElement() function (from the react package) to create an element. And now that you know what an element is from a technical point of view, let's inspect the value returned by createElement().
Inspecting an element
createElement() returns back a pure JavaScript object (i.e. inheriting from the Object interface) with a handful of properties.
Consider the following code:
let element = createElement(
'h1',
{ className: 'intro' },
'Introduction'
);
console.log(element);Here's what the console displays:
{
$$typeof: Symbol(react.element)
key: null
props: {className: 'heading', children: 'Hello World!'}
ref: null
type: 'h1'
_owner: null
}Alright, so there are a handful of properties to digest here:
typeis'h1', that is a string which implies that the element represents a DOM element.propscontains all the props provided to the element at the time of creation.refcontains an object holding the refs of the element.keydefines a unique key for the element, used to signal whether the element has changed or not (and therefore must be re-rendered completely).
The properties $$typeof and _owner are meant for internal use by React.
As this inspection illustrates, a React element is nothing more than a mere object with a bunch of properties. React uses theses objects in its virtual DOM tree in order to build the UI.
Immutability of elements
One important implementation detail of elements in React that's worth discussing is that of immutability. That is, React elements are immutable in nature. What does that mean?
It means that once an element is created, it can NOT be mutated, that is, you can't modify its existing properties or add new ones.
Consider the following code:
let element = createElement(
'h1',
{ className: 'intro' },
'Introduction'
);
element.props.x = 10;Here I'm trying to add a new prop x to the element by manually adding a property to the props object. However, since all elements are immutable in React, this leads to an error:
Uncaught TypeError: Cannot add property x, object is not extensiblex already exists on the element, you still couldn't modify its value using element.props.x.The rule is simple: If you wish to change the data of an element in React for some reason, you have to create a new element with the new set of data. An existing element is untouchable.
If you think about it, this rule isn't counter-intuitive. There's really almost no sense in modifying an existing React element by modifying its properties manually. Immutability is a virtue for React. But how?
Why are elements in React immutable?
Well, immutability prevents mutation and, likewise, prevents side effects from happening in a program. This, in turn, reduces the chances of errors in the program and makes development and testing much easier.
Immutability only in React's development build
Keep in mind that it's only when working with the development build of React that elements are immutable. The code above where I was adding a property to an element was executed in the development build and that's why I got the error.
In the production build, React elements are mutable. By letting go off its immutability enforcement, React improves performance in production.
Hence, if you run the same code above in React's production build, you won't run into any errors.
But this doesn't mean that you should mutate elements in the production build! A big no! You must always treat elements in React as immutable no matter which build you're currently in.