The syntax of JSX
<script type= "TEXT/JSX" > var hellomessage=react.createclass ({ render:function () { return <div Classname= "Test" >Hello{this.props.name}</div>; } ); React.render (
See this example, very simple, just implement the Render function, from the above code, we can see that jsx is actually JS, he and JS is the difference between the right to write HTML tags, which in the ordinary JS is impossible to achieve, The implementation can only be used in the form of strings to join the label, but in the JSX, it is possible to native support HTML tags.
The first knowledge point: HelloMessage, the element name, we write each component is actually an element name, here we declare a hellomessage tag, in the last row, we render it into the mountnode, we can see that When rendering the syntax is the standard HTML syntax, directly in the label to fill in the tag name, but this signature is our custom.
The second point of knowledge: is the child node This.props.name, tags and tags can have a nested relationship, as we write in HTML, each tag can be nested in other tags, he can also have a lot of tags as his child nodes, in Jsx, The difference between JSX and HTML nesting is that you can use evaluation expressions in child nodes, and we can see that the child nodes in the diagram are essentially a text node, except that the text node consists of two parts, the first part is the Hello string, followed by a space, The second part is an expression enclosed in curly braces, and what this expression does is take out the value of the Name property in the component's property, put it in this node, and hello+ the space into a complete text node, as to what is the attribute, we say later. As long as you know that each component has a property, that is, props, there are many properties and property names stored inside the property.
The third point of knowledge is the node attribute,
All-purpose function expressions
Normally, a function declaration is not an expression, but a statement, but we can change it to an expression in a special way, so that we can call the function directly to get the return value, because he is an expression, we can use it in curly braces, we look at the following example.
<! DOCTYPE html>This example should pay attention to the understanding of the force evaluation operation, that is, the parentheses that enclose the function, the function in the parentheses is forced to evaluate the operation, he will return a reference to a function, and then we added a later (this), with () to call him, And pass in a this, you can achieve the effect we want.
(function (obj) { }) (this)
This parenthesis also has a way of writing, is to put (this) in front of the parentheses in the back, specific to the code, it is also possible.
<! DOCTYPE html>The brackets are placed outside and inside the difference, when placed inside, the parentheses after the execution of the function to get a reference, and then call him, but the parentheses on the outside, the direct is the return value, rather than the function reference itself. We recommend that you look at the usage of this.