2. React JSX语法及特点介绍
什么是JSX
JSX的特点
如何使用JSX(JSX语法)
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>jsx注释</title>
<script src="../build/react.js"></script>
<script src="../build/react-dom.js"></script>
<script src="../build/browser.min.js"></script>
</head>
<body>
<div id="example"></div>
<script type="text/babel">
ReactDOM.render(
<h1
/*
注释1
*/
name = "test" // 注释2
>Hello, world!</h1>,
document.getElementById('example')
);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>jsx样式使用1className</title>
<script src="../build/react.js"></script>
<script src="../build/react-dom.js"></script>
<script src="../build/browser.min.js"></script>
<style type="text/css">
.text-red{
color : red
}
</style>
</head>
<body>
<div id="example"></div>
<script type="text/babel">
ReactDOM.render(
//元素不能直接写class因为class在es是关键字,无法被识别。要使用className替换原来的class属性。
//<h1 class='text-red'>Hello, world!</h1>,
<h1 className='text-red'>Hello, world!</h1>,
document.getElementById('example')
);
</script>
</body>
</html>
<body>
<div id="example"></div>
<script type="text/babel">
var style = {
color : "red",
//这里只能用驼峰命名不能用font-size
fontSize:'88px'
};
ReactDOM.render(
<h1 style={style}>Hello, world!</h1>,
//也可以在React中使用style
//<h1 style={{color:'red'}}>Hello, world!</h1>,
document.getElementById('example')
);
</script>
</body>
<body>
<script type="text/babel">
var HelloWorld = React.createClass({
render: function () {
return <p>Hello, {this.props.name ? this.props.name : "World"}</p>
}
});
//ReactDOM.render(<HelloWorld></HelloWorld>, document.body);
ReactDOM.render(<HelloWorld name='Jerome'></HelloWorld>, document.body);
</script>
</body>
<body>
<script type="text/babel">
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>
}
});
//ReactDOM.render(<HelloWorld></HelloWorld>, document.body);
ReactDOM.render(<HelloWorld name='Jerome'></HelloWorld>, document.body);
</script>
</body>
<body>
<script type="text/babel">
var HelloWorld = React.createClass({
getName: function () {
if (this.props.name)
return this.props.name
else
return "World"
},
render: function () {
return <p>Hello, {this.getName()}</p>
}
});
//ReactDOM.render(<div><HelloWorld></HelloWorld></div>, document.body);
ReactDOM.render(<HelloWorld name='Jerome'></HelloWorld>, document.body);
</script>
</body>
<body>
<script type="text/babel">
var HelloWorld = React.createClass({
render: function () {
// 左边如果有直就返回左边的,否则返回右边的。
return <p>Hello, {this.props.name || "World"}</p>
}
});
//ReactDOM.render(<div><HelloWorld></HelloWorld></div>, document.body);
ReactDOM.render(<HelloWorld name='Jerome'></HelloWorld>, document.body);
</script>
</body>
<body>
<script type="text/babel">
var HelloWorld = React.createClass({
render: function () {
return <p>Hello, {(function (obj) {
if (obj.props.name)
return obj.props.name
else
return "World"
})(this)}</p>
}
});
//ReactDOM.render(<div><HelloWorld></HelloWorld></div>, document.body);
ReactDOM.render(<HelloWorld name='Jerome'></HelloWorld>, document.body);
</script>
</body>
非DOM 属性介绍
<body>
<script type="text/babel">
// dangerouslySetInnerHTML:在JSX中直接插入HTML代码
var rawHTML = {
__html: "<h1>I'm inner HTML</h1>"
};
var HelloWorld = React.createClass({
render: function () {
return <p>Hello, World</p>
}
});
ReactDOM.render(<div dangerouslySetInnerHTML={rawHTML}></div>, document.body);
</script>
</body>
2. React JSX语法及特点介绍的更多相关文章
- React的jsx语法,详细介绍和使用方法!
jsx语法 一种混合使用html及javascript语法的代码 在js中 遇到<xx>即开始html语法 遇到</xx>则结束html语法 恢复成js语法 例如: let D ...
- React JSX语法说明
原文:http://my.oschina.net/leogao0816/blog/379487 什么是JSX? 在用React写组件的时候,通常会用到JSX语法,粗看上去,像是在Javascript代 ...
- React(JSX语法)-----JSX基本语法
JSX------HTML tags vs React Components: 1.To render a html tag,just use lower-case tag names in JSX; ...
- react.js 从零开始(三)JSX 语法及特点介绍
什么是jsx? jsx = JavaScript + xml jsx 是一种 Ecmascript 的一种新标准. jsx 是一种 带有结构性的语法. jsx 的特点: 1.类xml语法易于理解. 2 ...
- JSX语法及特点介绍
1.1 基本语法 1)自定义组件名首字母大写:元素名即组件名,首字母需要大写.首字母小写时React会以为这是HTML的标准标签,因此自定义的组件名需要首字母大写,否则会报错. 2)嵌套:在rende ...
- 学习 React(jsx语法) + es2015 + babel + webpack
视频学习地址: http://www.jtthink.com/course/play/575 官方地址 https://facebook.github.io/react/ 神坑: 1.每次this.s ...
- React(JSX语法)----动态UI
1.React honws how to bubble and capture events according to the spec,and events passed to your event ...
- React(JSX语法)----JSX拼写
注意:For DOM differences,such as the inline style attribute,check here. // bad: it displays "FIrs ...
- React(JSX语法)-----JSX属性
1. if you know all the propertities that you want to place on a component ahead of time,it is easy t ...
随机推荐
- 2015 多校联赛 ——HDU5384(AC自动机)
Sample Input 1 5 6 orz sto kirigiri danganronpa ooooo o kyouko dangan ronpa ooooo ooooo Sample Out ...
- bzoj1071[SCOI2007]组队
1071: [SCOI2007]组队 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 2472 Solved: 792[Submit][Status][ ...
- [BZOJ]1005 明明的烦恼(HNOI2008)
BZOJ的第一页果然还是很多裸题啊,小C陆续划水屯些板子. Description 自从明明学了树的结构,就对奇怪的树产生了兴趣......给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间 ...
- 浅谈MySQL中优化sql语句查询常用的30种方法
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索 ...
- c语言第六次作业v
(一)改错题 序列求和:输入一个正实数eps,计算序列部分和 1 - 1/4 + 1/7 - 1/10 + ... ,精确到最后一项的绝对值小于eps(保留6位小数). 输入输出样例: Input e ...
- TensorFlow官方文档
关于<TensorFlow官方文档> <TensorFlow官方文档>原文地址:http://devdocs.io/tensorflow~python/ ,本次经过W3Csch ...
- net use命令详解
net use命令详解 1)建立空连接: net use \\IP\ipc$ "" /user:"" (一定要注意:这一行命令中包含了3个空格) 2)建立非空连 ...
- 通过实例理解 RabbitMQ 的基本概念
先说下自己开发的实例. 最近在使用 Spring Cloud Config 做分布式配置中心(基于 SVN/Git),当所有服务启动后,SVN/Git 中的配置文件更改后,客户端服务读取的还是旧的配置 ...
- 解决$.ajax()在IE10以下失效的问题
一.问题重现 本地的脚本请求测试环境的接口 $.ajax({ url: ***, type: GET, data: data, success: function (data) { **** } }) ...
- js操作符+和()
ECMA-262 描述了一组用于操作数据值的操作符,包括一元操作符.算数操作符逻辑操作符.关系操作符.赋值操作符.字符串操作符.对象操作符等.ECMAScript 操作符的与众不同之处在于,它们能够适 ...