<script type="text/bebal"></script>
Detailed explanation of JSX syntax usage in React
This time I will bring you a detailed explanation of the use of JSX syntax in React. What are the precautions for the use of JSX syntax in React. The following is a practical case, let's take a look.
JSX syntax
A special js syntax sugar that can use html tags as objects in the code. It can be summarized into the following characteristics:
No addition Any quotation mark
used to be used as an html tag in js by adding quotation marks as string, but in jsx syntax, there is no need to add quotation marks, and it is used directly as an object
var html = <h1>React</h1>;
This paragraph of html tag can be parsed normally by the browser.
<input type="text" value="React">
var html = <input type="text" value="React">;
var html = <input type="text" value="React" />; var p = <p>React</p>;
var html = <p> <h1>Tom</h1> <h1>Lucy</h2> </p>
The following code adds comments to the tags, so an error will be reported.
var html = <p> <!--不能添加注释,这里会报错--> <h1>Tom</h1> <h1>Lucy</h2> </p>
javascript code to be mixed
In jsx syntax, if you want to use js code in html tags, use curly brackets ({ expression}) enclosed.var name = "DK"; var style = {fontSize: '12px', color: 'red'}; var html = <span style={style}>{name}</span>;
<span style="font-size:12px; color:red">DK</span>
<!--React 核心库-->
<script src="../../../../libs/react/react.js"></script>
<!--React 跟 Dom 相关的功能库-->
<script src="../../../../libs/react/react-dom.js"></script>
<!--babel 库,将 JSX 语法转为 JavaScript 语法-->
<script src="../../../../libs/react/browser.min.js"></script>
Copy after login
React is implemented based on jsx syntax, so it needs to be clear that the script type is text/babel<!--React 核心库--> <script src="../../../../libs/react/react.js"></script> <!--React 跟 Dom 相关的功能库--> <script src="../../../../libs/react/react-dom.js"></script> <!--babel 库,将 JSX 语法转为 JavaScript 语法--> <script src="../../../../libs/react/browser.min.js"></script>
<script type="text/bebal"></script>
Copy after login
Use React’s core objects and methods<script type="text/bebal"></script>
ReactDOM.render Render html tags to the specified container
<body>
<p id="p1"></p>
<p id="p2"></p>
<p id="p3"></p>
<!--jsx 语法-->
<script type="text/babel">
//将标签直接渲染到容器 p1 当中
ReactDOM.render(<h1>DK</h1>, document.getElementById('p1'));
var _style = {fontSize: '12px', color: 'red'};
var _name = "Tom";
var _obj = {name: "DK", age: 18};
//标签与 js 代码混写
ReactDOM.render(<h1 style={_style}>{_obj.age + (1 + 2)}</h1>, document.getElementById('p2'));
var array = ["Tom", "Lucy", "Lily"];
//多级标签和 js 代码混写
ReactDOM.render(
<p>
<ul>
{
array.map(function(arg1, arg2){
return <li key={arg2}>{arg1}</li>;
})
}
</ul>
<ul><li>Sam</li></ul>
<ul><li><input type="text" /></li></ul>
</p>,
document.getElementById('p3')
);
</script>
</body>
Copy after login
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website! Recommended reading:
<body> <p id="p1"></p> <p id="p2"></p> <p id="p3"></p> <!--jsx 语法--> <script type="text/babel"> //将标签直接渲染到容器 p1 当中 ReactDOM.render(<h1>DK</h1>, document.getElementById('p1')); var _style = {fontSize: '12px', color: 'red'}; var _name = "Tom"; var _obj = {name: "DK", age: 18}; //标签与 js 代码混写 ReactDOM.render(<h1 style={_style}>{_obj.age + (1 + 2)}</h1>, document.getElementById('p2')); var array = ["Tom", "Lucy", "Lily"]; //多级标签和 js 代码混写 ReactDOM.render( <p> <ul> { array.map(function(arg1, arg2){ return <li key={arg2}>{arg1}</li>; }) } </ul> <ul><li>Sam</li></ul> <ul><li><input type="text" /></li></ul> </p>, document.getElementById('p3') ); </script> </body>
Detailed explanation of the steps for using interfaces in JS
Detailed explanation of the implementation steps of PromiseA
The above is the detailed content of Detailed explanation of JSX syntax usage in React. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

Integration of Java framework and React framework: Steps: Set up the back-end Java framework. Create project structure. Configure build tools. Create React applications. Write REST API endpoints. Configure the communication mechanism. Practical case (SpringBoot+React): Java code: Define RESTfulAPI controller. React code: Get and display the data returned by the API.

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

PHP, Vue and React: How to choose the most suitable front-end framework? With the continuous development of Internet technology, front-end frameworks play a vital role in Web development. PHP, Vue and React are three representative front-end frameworks, each with its own unique characteristics and advantages. When choosing which front-end framework to use, developers need to make an informed decision based on project needs, team skills, and personal preferences. This article will compare the characteristics and uses of the three front-end frameworks PHP, Vue and React.

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

As a programming language widely used in the field of software development, C language is the first choice for many beginner programmers. Learning C language can not only help us establish the basic knowledge of programming, but also improve our problem-solving and thinking abilities. This article will introduce in detail a C language learning roadmap to help beginners better plan their learning process. 1. Learn basic grammar Before starting to learn C language, we first need to understand the basic grammar rules of C language. This includes variables and data types, operators, control statements (such as if statements,
