The versions above are only meant for development, and are not suitable for production. Minified and optimized production versions of React are available at:
This document will be most relevant to developers who work on frameworks, libraries, or developer tooling. Developers who use React primarily to build user-facing applications should not need to worry about our prerelease channels.
Each of React’s release channels is designed for a distinct use case:
Latest
is for stable, semver React releases. It’s what you get when you install React from npm. This is the channel you’re already using today.
Use this for all user-facing React applications.
Next
tracks the main branch of the React source code repository. Think of these as release candidates for the next minor semver release. Use this for integration testing between React and third party projects.
Experimental
includes experimental APIs and features that aren’t available in the stable releases. These also track the main branch, but with additional feature flags turned on. Use this to try out upcoming features before they are released.
All releases are published to npm, but only Latest uses semantic versioning. Prereleases (those in the Next and Experimental channels) have versions generated from a hash of their contents and the commit date, e.g.
0.0.0-68053d940-20210623
for Next and
0.0.0-experimental-68053d940-20210623
for Experimental.
The only officially supported release channel for user-facing applications is Latest
. Next and Experimental releases are provided for testing purposes only, and we provide no guarantees that behavior won’t change between releases. They do not follow the semver protocol that we use for releases from Latest.
By publishing prereleases to the same registry that we use for stable releases, we are able to take advantage of the many tools that support the npm workflow, like unpkg and CodeSandbox.
Latest Channel
Latest is the channel used for stable React releases. It corresponds to the
latest
tag on npm. It is the recommended channel for all React apps that are shipped to real users.
If you’re not sure which channel you should use, it’s Latest.
If you’re a React developer, this is what you’re already using.
You can expect updates to Latest to be extremely stable. Versions follow the semantic versioning scheme. Learn more about our commitment to stability and incremental migration in our versioning policy.
Next Channel
The Next channel is a prerelease channel that tracks the main branch of the React repository. We use prereleases in the Next channel as release candidates for the Latest channel. You can think of Next as a superset of Latest that is updated more frequently.
The degree of change between the most recent Next release and the most recent Latest release is approximately the same as you would find between two minor semver releases. However,
the Next channel does not conform to semantic versioning.
You should expect occasional breaking changes between successive releases in the Next channel.
Do not use prereleases in user-facing applications.
Releases in Next are published with the
next
tag on npm. Versions are generated from a hash of the build’s contents and the commit date, e.g.
0.0.0-68053d940-20210623
.
Using the Next Channel for Integration Testing
The Next channel is designed to support integration testing between React and other projects.
All changes to React go through extensive internal testing before they are released to the public. However, there are a myriad of environments and configurations used throughout the React ecosystem, and it’s not possible for us to test against every single one.
If you’re the author of a third party React framework, library, developer tool, or similar infrastructure-type project, you can help us keep React stable for your users and the entire React community by periodically running your test suite against the most recent changes. If you’re interested, follow these steps:
Set up a cron job using your preferred continuous integration platform. Cron jobs are supported by both CircleCI and Travis CI.
In the cron job, update your React packages to the most recent React release in the Next channel, using
next
tag on npm. Using the npm cli:
npm update react@next react-dom@next
Or yarn:
yarn upgrade react@next react-dom@next
Run your test suite against the updated packages.
If everything passes, great! You can expect that your project will work with the next minor React release.
If something breaks unexpectedly, please let us know by filing an issue.
A project that uses this workflow is Next.js. (No pun intended! Seriously!) You can refer to their CircleCI configuration as an example.
Experimental Channel
Like Next, the Experimental channel is a prerelease channel that tracks the main branch of the React repository. Unlike Next, Experimental releases include additional features and APIs that are not ready for wider release.
Usually, an update to Next is accompanied by a corresponding update to Experimental. They are based on the same source revision, but are built using a different set of feature flags.
Experimental releases may be significantly different than releases to Next and Latest.
Do not use Experimental releases in user-facing applications.
You should expect frequent breaking changes between releases in the Experimental channel.
Releases in Experimental are published with the
experimental
tag on npm. Versions are generated from a hash of the build’s contents and the commit date, e.g.
0.0.0-experimental-68053d940-20210623
.
What Goes Into an Experimental Release?
Experimental features are ones that are not ready to be released to the wider public, and may change drastically before they are finalized. Some experiments may never be finalized — the reason we have experiments is to test the viability of proposed changes.
For example, if the Experimental channel had existed when we announced Hooks, we would have released Hooks to the Experimental channel weeks before they were available in Latest.
You may find it valuable to run integration tests against Experimental. This is up to you. However, be advised that Experimental is even less stable than Next.
We do not guarantee any stability between Experimental releases.
How Can I Learn More About Experimental Features?
Experimental features may or may not be documented. Usually, experiments aren’t documented until they are close to shipping in Next or Latest.
If a feature is not documented, they may be accompanied by an RFC.
We will post to the React blog when we’re ready to announce new experiments, but that doesn’t mean we will publicize every experiment.
You can always refer to our public GitHub repository’s history for a comprehensive list of changes.
Click the link above to open an online editor. Feel free to make some changes, and see how they affect the output. Most pages in this guide will have editable examples like this one.
How to Read This Guide
In this guide, we will examine the building blocks of React apps: elements and components. Once you master them, you can create complex apps from small reusable pieces.
Tip
This guide is designed for people who prefer
learning concepts step by step
. If you prefer to learn by doing, check out our practical tutorial. You might find this guide and the tutorial complementary to each other.
This is the first chapter in a step-by-step guide about main React concepts. You can find a list of all its chapters in the navigation sidebar. If you’re reading this from a mobile device, you can access the navigation by pressing the button in the bottom right corner of your screen.
Every chapter in this guide builds on the knowledge introduced in earlier chapters.
You can learn most of React by reading the “Main Concepts” guide chapters in the order they appear in the sidebar.
For example, “Introducing JSX” is the next chapter after this one.
Knowledge Level Assumptions
React is a JavaScript library, and so we’ll assume you have a basic understanding of the JavaScript language.
If you don’t feel very confident, we recommend going through a JavaScript tutorial to check your knowledge level
and enable you to follow along this guide without getting lost. It might take you between 30 minutes and an hour, but as a result you won’t have to feel like you’re learning both React and JavaScript at the same time.
Note
This guide occasionally uses some newer JavaScript syntax in the examples. If you haven’t worked with JavaScript in the last few years, these three points should get you most of the way.
Let’s Get Started!
Keep scrolling down, and you’ll find the link to the next chapter of this guide right before the website footer.
This funny tag syntax is neither a string nor HTML.
It is called JSX, and it is a syntax extension to JavaScript. We recommend using it with React to describe what the UI should look like. JSX may remind you of a template language, but it comes with the full power of JavaScript.
React embraces the fact that rendering logic is inherently coupled with other UI logic: how events are handled, how the state changes over time, and how the data is prepared for display.
Instead of artificially separating
technologies
by putting markup and logic in separate files, React separates
concerns
with loosely coupled units called “components” that contain both. We will come back to components in a further section, but if you’re not yet comfortable putting markup in JS, this talk might convince you otherwise.
React doesn’t require using JSX, but most people find it helpful as a visual aid when working with UI inside the JavaScript code. It also allows React to show more useful error and warning messages.
With that out of the way, let’s get started!
Embedding Expressions in JSX
In the example below, we declare a variable called
name
and then use it inside JSX by wrapping it in curly braces:
const name ='Josh Perez';const element =<h1>Hello, {name}</h1>;
You can put any valid JavaScript expression inside the curly braces in JSX. For example,
2 + 2
,
user.firstName
, or
formatName(user)
are all valid JavaScript expressions.
In the example below, we embed the result of calling a JavaScript function,
formatName(user)
, into an
<h1>
element.
const user ={ firstName:'Harper', lastName:'Perez' };
const element =( <h1> Hello, {formatName(user)}!</h1> );
Try it on CodePen
We split JSX over multiple lines for readability. While it isn’t required, when doing this, we also recommend wrapping it in parentheses to avoid the pitfalls of automatic semicolon insertion.
JSX is an Expression Too
After compilation, JSX expressions become regular JavaScript function calls and evaluate to JavaScript objects.
This means that you can use JSX inside of
if
statements and
for
loops, assign it to variables, accept it as arguments, and return it from functions:
You may use quotes to specify string literals as attributes:
const element =<ahref="https://www.reactjs.org"> link </a>;
You may also use curly braces to embed a JavaScript expression in an attribute:
const element =<imgsrc={user.avatarUrl}></img>;
Don’t put quotes around curly braces when embedding a JavaScript expression in an attribute. You should either use quotes (for string values) or curly braces (for expressions), but not both in the same attribute.
Warning:
Since JSX is closer to JavaScript than to HTML, React DOM uses
camelCase
property naming convention instead of HTML attribute names.
For example,
class
becomes
className
in JSX, and
tabindex
becomes
tabIndex
.
Specifying Children with JSX
If a tag is empty, you may close it immediately with
/>
, like XML:
const element =<imgsrc={user.avatarUrl}/>;
JSX tags may contain children:
const element =( <div> <h1>Hello!</h1> <h2>Good to see you here.</h2> </div> );
JSX Prevents Injection Attacks
It is safe to embed user input in JSX:
const title = response.potentiallyMaliciousInput; // This is safe: const element =<h1>{title}</h1>;
By default, React DOM escapes any values embedded in JSX before rendering them. Thus it ensures that you can never inject anything that’s not explicitly written in your application. Everything is converted to a string before being rendered. This helps prevent XSS (cross-site-scripting) attacks.
JSX Represents Objects
Babel compiles JSX down to
React.createElement()
calls.
These two examples are identical:
const element =( <h1className="greeting"> Hello, world! </h1> );
const element = React.createElement( 'h1', {className:'greeting'}, 'Hello, world!' );
React.createElement()
performs a few checks to help you write bug-free code but essentially it creates an object like this:
// Note: this structure is simplified const element ={ type:'h1', props:{ className:'greeting', children:'Hello, world!' } };
These objects are called “React elements”. You can think of them as descriptions of what you want to see on the screen. React reads these objects and uses them to construct the DOM and keep it up to date.
We will explore rendering React elements to the DOM in the next section.
Tip:
We recommend using the “Babel” language definition for your editor of choice so that both ES6 and JSX code is properly highlighted.
Elements are the smallest building blocks of React apps.
An element describes what you want to see on the screen:
const element =<h1>Hello, world</h1>;
Unlike browser DOM elements, React elements are plain objects, and are cheap to create. React DOM takes care of updating the DOM to match the React elements.
Let’s say there is a
<div>
somewhere in your HTML file:
<divid="root"></div>
We call this a “root” DOM node because everything inside it will be managed by React DOM.
Applications built with just React usually have a single root DOM node. If you are integrating React into an existing app, you may have as many isolated root DOM nodes as you like.
To render a React element, first pass the DOM element to
ReactDOM.createRoot()
, then pass the React element to
root.render()
:
React elements are immutable. Once you create an element, you can’t change its children or attributes. An element is like a single frame in a movie: it represents the UI at a certain point in time.
With our knowledge so far, the only way to update the UI is to create a new element, and pass it to
root.render()
.
functiontick(){ const element =( <div> <h1>Hello, world!</h1> <h2>It is {newDate().toLocaleTimeString()}.</h2> </div> ); root.render(element);}
setInterval(tick,1000);
Try it on CodePen
It calls
root.render()
every second from a
setInterval()
callback.
Note:
In practice, most React apps only call
root.render()
once. In the next sections we will learn how such code gets encapsulated into stateful components.
We recommend that you don’t skip topics because they build on each other.
React Only Updates What’s Necessary
React DOM compares the element and its children to the previous one, and only applies the DOM updates necessary to bring the DOM to the desired state.
You can verify by inspecting the last example with the browser tools:
Even though we create an element describing the whole UI tree on every tick, only the text node whose contents have changed gets updated by React DOM.
In our experience, thinking about how the UI should look at any given moment, rather than how to change it over time, eliminates a whole class of bugs.
Components let you split the UI into independent, reusable pieces, and think about each piece in isolation. This page provides an introduction to the idea of components. You can find a
detailed component API reference here.
Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called “props”) and return React elements describing what should appear on the screen.
Function and Class Components
The simplest way to define a component is to write a JavaScript function:
This function is a valid React component because it accepts a single “props” (which stands for properties) object argument with data and returns a React element. We call such components “function components” because they are literally JavaScript functions.
You can also use an ES6 class to define a component:
The above two components are equivalent from React’s point of view.
Function and Class components both have some additional features that we will discuss in the next sections.
Rendering a Component
Previously, we only encountered React elements that represent DOM tags:
const element =<div/>;
However, elements can also represent user-defined components:
const element =<Welcomename="Sara"/>;
When React sees an element representing a user-defined component, it passes JSX attributes and children to this component as a single object. We call this object “props”.
For example, this code renders “Hello, Sara” on the page:
const root = ReactDOM.createRoot(document.getElementById('root')); const element =<Welcomename="Sara"/>;root.render(element);
Try it on CodePen
Let’s recap what happens in this example:
We call
root.render()
with the
<Welcome name="Sara" />
element.
React calls the
Welcome
component with
{name: 'Sara'}
as the props.
Our
Welcome
component returns a
<h1>Hello, Sara</h1>
element as the result.
React DOM efficiently updates the DOM to match
<h1>Hello, Sara</h1>
.
Note:
Always start component names with a capital letter.
React treats components starting with lowercase letters as DOM tags. For example,
<div />
represents an HTML div tag, but
<Welcome />
represents a component and requires
Welcome
to be in scope.
To learn more about the reasoning behind this convention, please read JSX In Depth.
Composing Components
Components can refer to other components in their output. This lets us use the same component abstraction for any level of detail. A button, a form, a dialog, a screen: in React apps, all those are commonly expressed as components.
For example, we can create an
App
component that renders
Welcome
many times:
Typically, new React apps have a single
App
component at the very top. However, if you integrate React into an existing app, you might start bottom-up with a small component like
Button
and gradually work your way to the top of the view hierarchy.
Extracting Components
Don’t be afraid to split components into smaller components.
It accepts
author
(an object),
text
(a string), and
date
(a date) as props, and describes a comment on a social media website.
This component can be tricky to change because of all the nesting, and it is also hard to reuse individual parts of it. Let’s extract a few components from it.
The
Avatar
doesn’t need to know that it is being rendered inside a
Comment
. This is why we have given its prop a more generic name:
user
rather than
author
.
We recommend naming props from the component’s own point of view rather than the context in which it is being used.
Extracting components might seem like grunt work at first, but having a palette of reusable components pays off in larger apps. A good rule of thumb is that if a part of your UI is used several times (
Button
,
Panel
,
Avatar
), or is complex enough on its own (
App
,
FeedStory
,
Comment
), it is a good candidate to be extracted to a separate component.
Props are Read-Only
Whether you declare a component as a function or a class, it must never modify its own props. Consider this
sum
function:
functionsum(a, b){ return a + b; }
Such functions are called “pure” because they do not attempt to change their inputs, and always return the same result for the same inputs.
In contrast, this function is impure because it changes its own input:
React is pretty flexible but it has a single strict rule:
All React components must act like pure functions with respect to their props.
Of course, application UIs are dynamic and change over time. In the next section, we will introduce a new concept of “state”. State allows React components to change their output over time in response to user actions, network responses, and anything else, without violating this rule.