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.
A special js syntax sugar that can use html tags as objects in the code. It can be summarized into the following characteristics:
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>
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>
<script type="text/bebal"></script>
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>
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!