- 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.
- Learn ECMAScript(Second Edition)
- Vue.js 3.x快速入門
- Power Up Your PowToon Studio Project
- 基于Java技術(shù)的Web應(yīng)用開發(fā)
- INSTANT MinGW Starter
- Windows Server 2016 Automation with PowerShell Cookbook(Second Edition)
- PLC應(yīng)用技術(shù)(三菱FX2N系列)
- 0 bug:C/C++商用工程之道
- Cocos2d-x Game Development Blueprints
- Java并發(fā)編程之美
- Python Web自動化測試設(shè)計與實現(xiàn)
- Python機器學習與量化投資
- Functional Python Programming
- Python高性能編程(第2版)
- C/C++代碼調(diào)試的藝術(shù)