一、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基础的更多相关文章

  1. React中最基础的jsx语法

    import React, { Component } from 'react'; class App extends Component { render() { return ( <div ...

  2. React Native 入门基础知识总结

    中秋在家闲得无事,想着做点啥,后来想想,为啥不学学 react native.在学习 React Native 时, 需要对前端(HTML,CSS,JavaScript)知识有所了解.对于JS,可以看 ...

  3. react的新手基础知识笔记

    <!DOCTYPE html> <html> <head> <script src="../build/react.js">< ...

  4. 前端笔记之React(一)初识React&组件&JSX语法

    一.React项目起步配置 官网:https://reactjs.org/ 文档:https://reactjs.org/docs/hello-world.html 中文:http://react.c ...

  5. React中JSX的理解

    React中JSX的理解 JSX是快速生成react元素的一种语法,实际是React.createElement(component, props, ...children)的语法糖,同时JSX也是J ...

  6. React Native JSX value should be expression or a quoted JSX text.

    问题描述:  我在使用props时候, 我的写法是这样的 ... <View> <Person name='john' age=32 gender=true></Pers ...

  7. [.net 面向对象编程基础] (3) 基础中的基础——数据类型

    [.net 面向对象编程基础] (3) 基础中的基础——数据类型 关于数据类型,这是基础中的基础. 基础..基础..基础.基本功必须要扎实. 首先,从使用电脑开始,再到编程,电脑要存储数据,就要按类型 ...

  8. [.net 面向对象编程基础] (4) 基础中的基础——数据类型转换

    [.net面向对象编程基础] (4)基础中的基础——数据类型转换 1.为什么要进行数据转换? 首先,为什么要进行数据转换,拿值类型例子说明一下, 比如:我们要把23角零钱,换成2.30元,就需要把整形 ...

  9. [.net 面向对象编程基础] (5) 基础中的基础——变量和常量

    [.net面向对象编程基础]  (5) 基础中的基础——变量和常量 1.常量:在编译时其值能够确定,并且程序运行过程中值不发生变化的量. 通俗来说,就是定义一个不能改变值的量.既然不能变动值,那就必须 ...

随机推荐

  1. Elastic数据迁移方法及注意事项

    需求 ES集群Cluster_A里的数据(某个索引或某几个索引),需要迁移到另外一个ES集群Cluster_B中. 环境 Linux:Centos7 / Centos6.5/ Centos6.4Ela ...

  2. day4_函数简单介绍

    一.函数是什么? 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,编程中的函数在英文中也有很多不同的叫法.在BASIC中叫做subroutine(子过程或子程序),在Pasc ...

  3. LeetCode 1012 Complement of Base 10 Integer 解题报告

    题目要求 Every non-negative integer N has a binary representation.  For example, 5 can be represented as ...

  4. python摸爬滚打之day14----内置函数,递归函数

    1.匿名函数 用一句话实现的简单函数. ret = lambda x : x ** 2      即 函数名 = lambda 形参 : 返回值 print(ret(5))  ----> 25 ...

  5. ES6的十大新特性(转)

    add by zhj: 该文章是由国外一哥们写的,由腾讯前端团队翻译,图片中的妹子长得挺好看的,很养眼,嘿嘿.我目前在学习ES6,这篇文章把ES6的 几个主要新特性进行了归纳总结,犹如脑图一般,让人看 ...

  6. 前端 HTML body标签相关内容 常用标签 标题标签 h1-h6

    标题标签 h1~h6 <h1> - <h6> 标签可定义标题.<h1> 定义最大的标题.<h6> 定义最小的标题. 由于 h 元素拥有确切的语义,因此请 ...

  7. Visual Studio使用Web Deploy远程发布网站及其配置

    https://blog.csdn.net/yzj_xiaoyue/article/details/60574378 废话不多说,直接进入正题(各个步骤请看图片的序号): IIS配置 1.打开服务器 ...

  8. 用PowerDesigner建立概念模型的问题:不能创建相同字段名的关键字段

    依次点击Tools--->Model Options->Model Settings,在Model Settings中有Data Item组框,取消里面的Unique Code,勾选All ...

  9. testetest

    resumeLoad renren静态 foolday \ swImg activity01

  10. JsonResponse返回中文乱码问题

    class Publish(APIView): def get(self, request): publish_list = models.Publish.objects.all() bs = MyS ...