1.首字母大小写
2.嵌套
3.求值表达式
4.htmlFor和className
5.驼峰命名
M1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello World</title>
</head>
<body>
<script src="./react.js"></script>
<script src="./JSXTransformer.js"></script>
<script type="text/jsx">
var style = {
color:"red"
}
var HelloWorld = React.createClass({
render: function () {
return <p>hello {this.props.name?this.props.name:"world"}</p>
}
})
React.render(<div style={style}><HelloWorld name="Quincy"></HelloWorld></div>, document.body)
</script>
</body>
</html>
M2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello World</title>
</head>
<body>
<script src="./react.js"></script>
<script src="./JSXTransformer.js"></script>
<script type="text/jsx">
var style = {
color: "red"
}
var HelloWorld = React.createClass({
getName: function () {
if (this.props.name){
return this.props.name
}
else{
return "World"
}
},
render: function () {
var name = this.getName()
return <p>hello {name}</p>
}
})
React.render(<div style={style}><HelloWorld name="Quincy"></HelloWorld></div>, document.body)
</script>
</body>
</html>
M3
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello World</title>
</head>
<body>
<script src="./react.js"></script>
<script src="./JSXTransformer.js"></script>
<script type="text/jsx">
var style = {
color: "red"
}
var HelloWorld = React.createClass({
render: function () {
//如果左边为真返回左边,如果左边为假则返回右边
return <p>hello {this.props.name||"World"}</p>
}
})
React.render(<div style={style}><HelloWorld name="Quincy"></HelloWorld></div>, document.body)
</script>
</body>
</html>
万能函数表达式(闭包函数)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello World</title>
</head>
<body>
<script src="./react.js"></script>
<script src="./JSXTransformer.js"></script>
<script type="text/jsx">
var style = {
color: "red"
}
var HelloWorld = React.createClass({
render: function () {
return <p>hello {(function (obj) {
if (obj.props.name)
return obj.props.name
else
return 'World'
})(this)}</p>
}
})
React.render(<div style={style}><HelloWorld></HelloWorld></div>, document.body)
</script>
</body>
</html>