ReactJS is a JavaScript library contained in a single file react-<version>.js
that can be included in any HTML page. People also commonly install the React DOM library react-dom-<version>.js
along with the main React file:
Basic Inclusion
<!DOCTYPE html>
<html>
<head></head>
<body>
<script type="text/javascript" src="/path/to/react.js"></script>
<script type="text/javascript" src="/path/to/react-dom.js"></script>
<script type="text/javascript">
// Use react JavaScript code here or in a separate file
</script>
</body>
</html>
To get the JavaScript files, go to the installation page of the official React documentation.
React also supports JSX syntax. JSX is an extension created by Facebook that adds XML syntax to JavaScript. In order to use JSX you need to include the Babel library and change <script type="text/javascript">
to <script type="text/babel">
in order to translate JSX to Javascript code.
<!DOCTYPE html>
<html>
<head></head>
<body>
<script type="text/javascript" src="/path/to/react.js"></script>
<script type="text/javascript" src="/path/to/react-dom.js"></script>
<script src="https://npmcdn.com/[email protected]/browser.min.js"></script>
<script type="text/babel">
// Use react JSX code here or in a separate file
</script>
</body>
</html>
Installing via npm
You can also install React using npm by doing the following:
npm install --save react react-dom
To use React in your JavaScript project, you can do the following:
var React = require('react');
var ReactDOM = require('react-dom');
ReactDOM.render(<App />, ...);
Installing via Yarn
Facebook released its own package manager named Yarn, which can also be used to install React. After installing Yarn you just need to run this command:
yarn add react react-dom
You can then use React in your project in exactly the same way as if you had installed React via npm.