Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Progressive Web Apps with React

You're reading from   Progressive Web Apps with React Create lightning fast web apps with native power using React and Firebase

Arrow left icon
Product type Paperback
Published in Oct 2017
Publisher Packt
ISBN-13 9781788297554
Length 302 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Scott Domes Scott Domes
Author Profile Icon Scott Domes
Scott Domes
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Creating Our App Structure FREE CHAPTER 2. Getting Started with Webpack 3. Our App's Login Page 4. Easy Backend Setup With Firebase 5. Routing with React 6. Completing Our App 7. Adding a Service Worker 8. Using a Service Worker to Send Push Notifications 9. Making Our App Installable with a Manifest 10. The App Shell 11. Chunking JavaScript to Optimize Performance with Webpack 12. Ready to Cache 13. Auditing Our App 14. Conclusion and Next Steps

Welcome to ReactDOM

Here's a secret about React--it's a library for creating UIs, but not a library for rendering UIs. In itself, it has no mechanism for rendering a UI to the browser.

Fortunately, the creators of React also have a package called ReactDOM for exactly this purpose. Let's install it and then see how it works.

First, we install it with yarn add [email protected].

Then, require it in index.html in much the same way as React:

<body>
<img src="assets/icon.png" id="test-image"/>
<h1>Hello world!</h1>
<div id="root"></div>
<script src="../node_modules/react/dist/react.js"></script>
<script src="../node_modules/react-dom/dist/react-dom.js"></script>
<script>
console.log(React.createElement('h1', null, 'Hello from react!'));
</script>
</body&gt;

ReactDOM has a function called render, which takes two arguments: the React element to be rendered to the screen (hey, we have that already!), and the HTML element it will be rendered inside.

So, we have the first argument, but not the second. We need something in our existing HTML we can grab and hook into; ReactDOM will inject our React element inside of it.

So, below our existing <h1> tag, create an empty div with the ID root.

Then, in our ReactDOM.render function, we’ll pass in the React element, and then use document.getElementById to grab our new div.

Here's what our index.html should look like:

<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<link rel="stylesheet" href="assets/app.css">
<link rel="shortcut icon" href="assets/favicon.ico" type="image/x-icon">
</head>
<body>
<img src="assets/icon.png" id="test-image"/>
<h1>Hello world!</h1>
<div id="root"></div>
<script src="../node_modules/react/dist/react.js"></script>
<script src="../node_modules/react-dom/dist/react-dom.js"></script>
<script>
ReactDOM.render(React.createElement('h1', null, 'Hello from react!'),
document.getElementById('root'));

</script>
</body>
</html>

Reload the page, and you should see 'Hello from React!' text in the middle of the screen!

You have been reading a chapter from
Progressive Web Apps with React
Published in: Oct 2017
Publisher: Packt
ISBN-13: 9781788297554
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image