react基础&JSX基础
一、HTML 标签 vs. React 组件
React 可以渲染 HTML 标签 (strings) 或 React 组件 (classes)。
1、要渲染 HTML 标签,只需在 JSX 里使用小写字母的标签名。
var myDivElement = <div className="foo" />;
ReactDOM.render(myDivElement, document.getElementById('example'));
2、要渲染 React 组件,只需创建一个大写字母开头的本地变量。
var MyComponent = React.createClass({/*...*/});
var myElement = <MyComponent someProperty={true} />;
ReactDOM.render(myElement, document.getElementById('example'));
3、React 的 JSX 使用大、小写的约定来区分本地组件的类和 HTML 标签。
二、
1、在标签内部的注释需要花括号
2、在标签外的的注释不能使用花括号
例:
ReactDOM.render(
/*注释 */
<h1>detanx {/*注释*/}</h1>,
document.getElementById('example')
);
三、JSX中不能使用if else,可以使用三元运算符代替
例:
<h1 style={mystyle}>{1 > 2? 'true':'false'}</h1>
四、react组件
例:
var HelloMessage = React.createClass({
render: function() {
return <h1>Hello World!</h1>;
}
});
ReactDOM.render(
<HelloMessage />,
document.getElementById('example')
);
需要向组件传递参数,可以使用 this.props 对象
例:
var HelloMessage = React.createClass({
render: function() {
return <h1>Hello {this.props.name}</h1>;
}
});
/*方法二
function HelloMessage(props){
return <h1>Hello,{props.name}</h1>;
}*/
ReactDOM.render( <HelloMessage name="Runoob" />, document.getElementById('example') );
通过 getDefaultProps() 方法为 props 设置默认值
var HelloMessage = React.createClass({
getDefaultProps: function() {
return {
name: 'Runoob'
};
},
render: function() {
return <h1>Hello {this.props.name}</h1>;
}
});
ReactDOM.render(
<HelloMessage />,
document.getElementById('example')
);
name 属性通过 this.props.name 来获取。
注意:在添加属性时, class 属性需要写成 className ,for 属性需要写成 htmlFor ,
这是因为 class 和 for 是 JavaScript 的保留字。
五、复合组件
例:
var WebSite = React.createClass({
render: function() {
return (
<div>
<Name name={this.props.name} />
<Link site={this.props.site} />
</div>
);
}
});
var Name = React.createClass({
render: function() {
return (
<h1>{this.props.name}</h1>
);
}
});
var Link = React.createClass({
render: function() {
return (
<a href={this.props.site}>
{this.props.site}
</a>
);
}
});
ReactDOM.render(
<WebSite name="detanx" site=" http://www.baidu.com" />,
document.getElementById('example')
);
注意:ReactDOM.render(<组建名 />);组件名是用单标签< />即带斜杠"/"的尖括号包围的
react基础&JSX基础的更多相关文章
- React中最基础的jsx语法
import React, { Component } from 'react'; class App extends Component { render() { return ( <div ...
- React Native 入门基础知识总结
中秋在家闲得无事,想着做点啥,后来想想,为啥不学学 react native.在学习 React Native 时, 需要对前端(HTML,CSS,JavaScript)知识有所了解.对于JS,可以看 ...
- react的新手基础知识笔记
<!DOCTYPE html> <html> <head> <script src="../build/react.js">< ...
- 前端笔记之React(一)初识React&组件&JSX语法
一.React项目起步配置 官网:https://reactjs.org/ 文档:https://reactjs.org/docs/hello-world.html 中文:http://react.c ...
- React中JSX的理解
React中JSX的理解 JSX是快速生成react元素的一种语法,实际是React.createElement(component, props, ...children)的语法糖,同时JSX也是J ...
- React Native JSX value should be expression or a quoted JSX text.
问题描述: 我在使用props时候, 我的写法是这样的 ... <View> <Person name='john' age=32 gender=true></Pers ...
- [.net 面向对象编程基础] (3) 基础中的基础——数据类型
[.net 面向对象编程基础] (3) 基础中的基础——数据类型 关于数据类型,这是基础中的基础. 基础..基础..基础.基本功必须要扎实. 首先,从使用电脑开始,再到编程,电脑要存储数据,就要按类型 ...
- [.net 面向对象编程基础] (4) 基础中的基础——数据类型转换
[.net面向对象编程基础] (4)基础中的基础——数据类型转换 1.为什么要进行数据转换? 首先,为什么要进行数据转换,拿值类型例子说明一下, 比如:我们要把23角零钱,换成2.30元,就需要把整形 ...
- [.net 面向对象编程基础] (5) 基础中的基础——变量和常量
[.net面向对象编程基础] (5) 基础中的基础——变量和常量 1.常量:在编译时其值能够确定,并且程序运行过程中值不发生变化的量. 通俗来说,就是定义一个不能改变值的量.既然不能变动值,那就必须 ...
随机推荐
- 网上常用免费WebServices集合
天气预报Web服务,数据来源于中国气象局 公用事业http://www.webxml.com.cn/WebServices/WeatherWebService.asmx 中国股票行情分时走势预览缩略图 ...
- Linux上mount 挂载windows共享文件权限问题
在服务器部署的时候需要把文件夹设置在windows的共享文件上.在使用mount命令挂载到linux上后.文件路径和文件都是可以访问,但是不能写入,导致系统在上传文件的时候提示“权限不够,没有写权限” ...
- [centos][ntp][administrator] chrony ntp
以下内容,适用于 CentOS 7 (systemd 体系) 一. 首先,确认你是否启用了 ntp 服务: [root@nlb2-liantiao ~]# timedatectl Local time ...
- Java代码中获取Json的key值
测试json字符串: {"access_token":"hkbQl5o_l67dZ7_vJRATKBwTLk9Yj5QyMuOJThAr8Baj0xWf4wxW1p4ym ...
- 【PyQt5-Qt Designer】工具箱(QToolBox)控件的使用
工具箱(QToolBox)+toolButton+tabWidget 总体介绍 QToolBox类提供了一列选项卡的小部件(选项卡内含项目). 工具箱是一个小部件,它将选项卡一个一个的显示,当前项目显 ...
- es定制排序搜索结果
GET /company/employee/_search { "query": { "constant_score": { "filter" ...
- C#-1-1-.net
1..Net (1).Net平台 (2). FrameWork框架 2..Net应用 (1).桌面应用程序(Win form(.Net开发的桌面应用程序叫做Win form应用程序)). (2).In ...
- 【托业】【新托业TOEIC新题型真题】学习笔记1--题库一-->P1~4
P1~4--------------------------------------单词-------------------------------------- status 雕像 couple ...
- 微信公开课厦门站 时尚行业专场PPT
做为一位开发者,ytkah有幸参加了微信公开课厦门站-时尚行业专场,见证了微信支付的发展历程,小程序产品的实力简介,感受了一下与各位高手共聚一堂的氛围,当然还近距离接触了著名主持人兼NPC潮品(与潘玮 ...
- IE无法安装Activex控件
由于无法验证发行者,所以windows已经阻止此软件,如要安装未签名的activex控件,按如下步骤: 1.打开Internet Explorer---菜单栏点“工具”---Internet选项--安 ...