MOCKSTACKS
EN
Questions And Answers

More Tutorials









React Hello World


Without JSX


Here's a basic example that uses React's main API to create a React element and the React DOM API to render the React element in the browser.

<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8" />
 <title>Hello React!</title>
 <!-- Include the React and ReactDOM libraries -->
 <script src="https://fb.me/react-15.2.1.js"></script>
 <script src="https://fb.me/react-dom-15.2.1.js"></script>
 </head>
 <body>
 <div id="example"></div>
 <script type="text/javascript">
 // create a React element rElement
 var rElement = React.createElement('h1', null, 'Hello, world!');
 // dElement is a DOM container
 var dElement = document.getElementById('example');
 // render the React element in the DOM container
 ReactDOM.render(rElement, dElement);
 </script>
 </body>
</html>

With JSX


Instead of creating a React element from strings one can use JSX (a Javascript extension created by Facebook for adding XML syntax to JavaScript), which allows to write

var rElement = React.createElement('h1', null, 'Hello, world!');

as the equivalent (and easier to read for someone familiar with HTML)

var rElement = <h1>Hello, world!</h1>;

The code containing JSX needs to be enclosed in a
<script type="text/babel">
tag. Everything within this tag will be transformed to plain Javascript using the Babel library (that needs to be included in addition to the React libraries).

So finally the above example becomes:

<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8" />
 <title>Hello React!</title>
 <!-- Include the React and ReactDOM libraries -->
 <script src="https://fb.me/react-15.2.1.js"></script>
 <script src="https://fb.me/react-dom-15.2.1.js"></script>
 <!-- Include the Babel library -->
 <script src="https://npmcdn.com/babel-core@5.8.38/browser.min.js"></script>
 </head>
 <body>
 <div id="example"></div>
 <script type="text/babel">
 // create a React element rElement using JSX
 var rElement = <h1>Hello, world!</h1>;
 // dElement is a DOM container
 var dElement = document.getElementById('example');
 // render the React element in the DOM container
 ReactDOM.render(rElement, dElement);
 </script>
 </body>
</html>


Conclusion

In this page (written and validated by ) you learned about React Hello World . What's Next? If you are interested in completing React tutorial, your next topic will be learning about: React Hello World Component.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.