- Progressive Web Apps with React
- Scott Domes
- 284字
- 2021-07-08 09:36:16
Using React
Let's confirm that React is in our project by using it to render a simple element to our screen. This will be our first dipping of our feet into React, so go slow and ensure that you understand each step.
First, we need to import our React package (which we just installed with yarn) into our index.html so that we can use it there.
To do this, we add a <script> tag with the path to the main React file within our node-modules folder. This tag looks like this:
<script src="../node_modules/react/dist/react.js"></script>
Place this in your index.html, at the bottom of the body tag (before the closing </body>).
Okay, we have React! Let's use it to make a simple <h1> tag, just like the one we wrote in HTML.
React has a function called createElement for this purpose. It takes three arguments: element type, something called props (more on that later), and the children (what goes inside the tag).
For us, it looks like this:
React.createElement('h1', null, 'Hello from React!')
This function call creates an element that looks as follows:
<h1>Hello from React!</h1>
To confirm that it will work, let's console.log it out:
<script src="../node_modules/react/dist/react.js"></script>
<script>
console.log(React.createElement('h1', null, 'Hello from react!'))
</script>
Reload index.html, then right-click or control-click and select Inspect to open up DevTools in Chrome and switch to the Console tab. There, we see our element… or not. Instead of the HTML output, we get something like this:

This is not the HTML element we might have expected, but we can see that React has worked in its own way. We have a JavaScript object with a type field of h1. Let’s see whether we can transform this into an actual HTML tag on the screen.
- 工程軟件開發(fā)技術基礎
- FreeSWITCH 1.6 Cookbook
- Web Application Development with MEAN
- 征服RIA
- C語言實驗指導及習題解析
- Nexus規(guī)模化Scrum框架
- Learning Zurb Foundation
- AIRIOT物聯網平臺開發(fā)框架應用與實戰(zhàn)
- 微信小程序開發(fā)與實戰(zhàn)(微課版)
- Learning YARN
- 零基礎學C語言程序設計
- ASP.NET Web API Security Essentials
- PhoneGap 4 Mobile Application Development Cookbook
- Python Deep Learning
- 川哥教你Spring Boot 2實戰(zhàn)