React (sometimes called React.js or ReactJS) is an open-source JavaScript library for rendering data as HTML views. React views are typically rendered using components that contain other components defined by custom HTML tags. React provides a model for programmers where child components cannot directly affect outer components (“data flows down”), allowing for efficient updates to the HTML document when data changes and clean separation of components in modern single-page applications. (Wikipedia)
JSX File
JSX is an extension of JavaScript (JS) and its essence is still JS.
JSX files are compiled using the babel compiler.
Syntax Format of JSX File
Before compilation, the syntax is the same as HTML.
1
<div>hello</div>
After compilation, it is essentially a JS file.
1
React.createElement("div", null, "hello");
Introduction of Compiler
Import the babel compiler into the html file to automatically compile the JSX syntax code.
1 2 3 4
<scriptsrc="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script> <scripttype="text/babel"> let html = <div>hello</div>; </script>
Manually Compile JSX File
Manually compile the JSX file using the babel compiler.
<input>: The .jsx file before compilation. <output>: The .js file after compilation.
1
npx babel <input>.jsx -o <output>.js
JSX Expression
JSX itself is an expression.
Whether it is in JSX content, attribute, or JSX itself, JSX expressions can be used.
1 2 3
let name = ""; let jsx = <div>{name}</div>; let jsx = <divname={name}></div>;
className
class cannot be used in JSX to add class names to elements because JSX is essentially JS and class is a keyword in JS. Therefore, className should be used instead of class to add class names to elements.
1
let jsx = <div className="div"></div>;
Automatic Expansion of Arrays
JSX automatically iterates through arrays.
1 2 3 4 5 6 7
let arrs = [ <divkey="1">1</div>, <divkey="2">2</div>, <divkey="3">3</div> ] let jsx = <div>{arrs}</div> ReactDOM.render(jsx, document.getElementById("app"));
Operators
JSX syntax also supports operators.
Display data dynamically based on boolean values.
Ternary Operator
1 2 3
let bool = true; let jsx = <div>{bool?<div>hello</div>:null}</div>; ReactDOM.render(jsx, document.getElementById("app"));
AND Operator
1 2 3
let bool = true; let jsx = <div>{bool&&<div>hello</div>}</div>; ReactDOM.render(jsx, document.getElementById("app"));
Loops
In JSX, loops should not use for, but should use map.
A component can also be created directly through JSX code.
Returns a virtual object.
First parameter: tag name Second parameter: tag attributes Third parameter: tag content
1
React.createElement("div", null, "hello");
翻译结果:
Preface
React (sometimes called React.js or ReactJS) is an open-source JavaScript library that renders data into HTML views. React views are usually rendered using components that include other components defined by custom HTML tags. React provides a model for programmers where child components cannot directly influence outer components (“data flows down”), enabling effective updates to the HTML document when data changes and clean separation between components in modern single-page applications. (Wikipedia)
JSX Files
JSX is an extension of JavaScript (JS) and its essence is still JS.
JSX files are compiled using the babel compiler.
Syntax Format of JSX Files
Before compilation, the syntax is similar to HTML.
1
<div>hello</div>
After compilation, it essentially becomes a JS file.
1
React.createElement("div", null, "hello");
Importing Compiler
Import the babel compiler into the HTML file to automatically compile JSX syntax code.
1 2 3 4
<scriptsrc="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script> <scripttype="text/babel"> let html = <div>hello</div>; </script>
Manually Compiling JSX Files
Manually compile JSX files using the babel compiler.
<input>: The .jsx file before compilation <output>: The .js file after compilation
1
npx babel <input>.jsx -o <output>.js
JSX Expressions
JSX itself is an expression.
JSX expressions can be used in JSX content, attributes, or JSX itself.
1 2 3
let name = ""; let jsx = <div>{name}</div>; let jsx = <divname={name}></div>;
className
class cannot be used in JSX to add class names to elements because JSX is essentially JS, and class is a keyword in JS. Instead, use className to add class names to elements.
1
let jsx = <div className="div"></div>;
Automatic Array Expansion
JSX automatically loops through arrays.
1 2 3 4 5 6 7
let arrs = [ <divkey="1">1</div>, <divkey="2">2</div>, <divkey="3">3</div> ] let jsx = <div>{arrs}</div> ReactDOM.render(jsx, document.getElementById("app"));
Operators
JSX syntax also supports operators.
Display data dynamically based on boolean values.
Ternary Operator
1 2 3
let bool = true; let jsx = <div>{bool?<div>hello</div>:null}</div>; ReactDOM.render(jsx, document.getElementById("app"));
AND Operator
1 2 3
let bool = true; let jsx = <div>{bool&&<div>hello</div>}</div>; ReactDOM.render(jsx, document.getElementById("app"));