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 ...
随机推荐
- bzoj1853[Scoi2010]幸运数字 容斥
1853: [Scoi2010]幸运数字 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 3027 Solved: 1128[Submit][Status ...
- python 类的特殊成员方法
__doc__ # 输出类的描述信息 __module__ # 表示当前操作的对象在那个模块 __class__ # 表示当前操作的对象的类是什么 __init__ # 构造方法,通过类创建对象是,自 ...
- SqlServer查询优化方法
MS SQL Server查询优化方法查询速度慢的原因很多,常见如下几种 文章来源http://bbs.csdn.net/topics/250004467 1.没有索引或者没有用到索引(这是查询慢最常 ...
- DDD实战进阶第一波(七):开发一般业务的大健康行业直销系统(实现产品上下文接口与测试)
前一篇文章我们介绍了如何将创建产品的领域逻辑与产品的持久化仓储通过上架产品的用例组织起来,完成了一个功能.在实际的项目中,多种前端的形态比如PC Web. 微信小程序.原生APP等要调用后端的功能,通 ...
- pupeteer初体验
官方文档: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagescreenshotoptions puppet ...
- React框架 dva 和 mobx 的使用感受
最近在用react写web项目,领导为了让前端便于维护要求都用react作为开发基础,框架选型不限.在使用 react 的时候或多或少会接触到状态管理,从开始学 react 到现在也挺久了,做一些前端 ...
- 70. Climbing Stairs(easy, 号称 Dynamic Programming 天下第一题)
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- C#利用Attribute实现简易AOP介绍
首先看一段简单的代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } //来自UI层的调用 ...
- python if判断语句&计算
python对缩进要求严格,代码块里的缩进必须一样,可以常用 tab键 表示4个空格 if 条件: 代码块 else: if判断语句如下: 1 print("吃饭,喝水,回家") ...
- JS中的DOM— —节点以及操作
DOM操作在JS中可以说是非常常见了吧,很多网页的小功能的实现,比如一些元素的增删操作等都可以用JS来实现.那么在DOM中我们需要知道些什么才能完成一些功能的实现呢?今天这篇文章就先简单的带大家入一下 ...