react

引入 jsx 语法内容时,需要指定 type为 text/babel
<script src="./jsx.js" type="text/babel"></script>
或者
<script type="text/babel">
...jxs
</script>
ReactDOM.render(html,target[,callback])将内容和渲染到指定的节点
变量用法
{ }代表进入了JavaScript执行环境

//基础用法
const a = <h1>Hello React</h1>;
// 变量用法
//{ }代表进入了JavaScript执行环境
let msg = "Hello React";
let href  = "http://www.baidu.com";
const b = <a href={href}>{msg}</a>
//*ReactDOM.render(html,target[,callback])*将内容和渲染到指定的节点
ReactDOM.render(
    <div>
        {a}
        {b}
    </div>,
    document.querySelector(".box")
)

jsx 标签必须要有结束标签。<a></a><input/>
jsx 中注释必须用{ 包裹}
只有一个根节点

//基础用法
const a = <h1>Hello React</h1>;
// 变量用法
//{ }代表进入了JavaScript执行环境
let msg = "Hello React";
let href  = "http://www.baidu.com";
const b = <a href={href}>{msg}</a>
//*ReactDOM.render(html,target[,callback])*将内容和渲染到指定的节点
const c = React.createElement("a",{href:"http://www.baidu.com"},"复杂超链接")

const d = React.DOM.a({href:"http://www.baidu.com"},"复杂的超链接D");

const e =   <div>
                <h1>嵌套</h1>
                <span> 数据</span>
            </div>;

const f = React.createElement("div",null,
    React.createElement("h1",null,"嵌套二")
);
//书写style时,横线式要改为驼峰式,font-size=>fontSize
const g = <span style={{color:'red',fontSize:'20px'}}>Style 写法</span>

const so = {
    color:'green'
}

const h = <span style={so}>STYLE</span>;
//ES6中使用class关键字 声明了类
//对于一些关键字要进行转换, class=>className  label的 for=>htmlFor
const i = <span className="cn"> Class 写法</span>;
const j = [
    <h3 key="1"> 数组1 </h3>,
    <h3 key='2'> 数组2 </h3>,
    <h3 key='3'> 数组3 </h3>
];
const k = <div>
    <hr/>
    <h3>Hello</h3>
    {j}
</div>
const l = ['数组4','数组5','数组6']
ReactDOM.render(
    <div>
        {/*这是一段注释*/}
        {a}
        {b}
        {c}
        {d}
        {e}
        {g}
        {f}
        {h}
        {i}
        {j}
        {k}
        {l}
        {
            l.map((m,n)=>{
                return <h1 key={n}>{m}</h1>
            })
        }
    </div>,
    document.querySelector(".box")
)
// Array.map(function(item,index){})
map/forEach/for/filter

组件化



//ES5 的React.createClaa()终将被弃用,请尽量使用ES6的写法创建组件
    // 由于继承的子类没有this,所以在ES6中需要使用constructor 得到 this
    // 而在 ES5 中,createClass 所创建的类将自动拥有 this,可直接使用this.props
    // this.props 将得到父级向下传递的数据
//this.props.children 得到组件的原始内容(子元素)
    //当有一个子元素时,返回对象
    //当有多个子元素时,返回数组
    //当没有子元素时,返回 undefined
const Com1 = React.createClass({
    render(){
        return <div>
                <h1>Hello ES5 React Component!!!</h1>
                <h3>{this.props.msg}</h3>
            </div>
    }
}); 
//ES6
class Com2 extends React.Component{ //继承的类不能使用this
    constructor(props){//props 接收 传过来的值 
        super(props);
    }
    render(){
        return <div>
            <h1>Hello ES6 react component!!!</h1>
            <h3>{ this.props.msg }</h3>
         </div>
    }
}
ReactDOM.render(
    <div>
        <h1>Hello react</h1>
        <Com1 msg="你好"></Com1>
        <Com2 msg="你好"></Com2>
          {/*<Com2/>*/}
    </div>,
    document.querySelector(".box")
)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 原教程内容详见精益 React 学习指南,这只是我在学习过程中的一些阅读笔记,个人觉得该教程讲解深入浅出,比目前大...
    leonaxiong阅读 7,846评论 1 18
  • 现在最热门的前端框架,毫无疑问是 React 。上周,基于 React 的 React Native 发布,结果一...
    sakura_L阅读 3,180评论 0 0
  • 最近看了一本关于学习方法论的书,强调了记笔记和坚持的重要性。这几天也刚好在学习React,所以我打算每天坚持一篇R...
    gaoer1938阅读 5,674评论 0 5
  • 安装: 概述 React起源于FaceBook的内部项目,因为该公司对市场上所有的JavaScript MVC框架...
    姒沝無痕阅读 4,023评论 0 0
  • 我的外婆, 70出头,她会用手机发微信语音;我的姨妈,50出头,天天拿ipad看剧,我的外甥,7岁,每次见我,总问...
    一步王钰晨阅读 3,720评论 0 1