美文网首页
react怎么构建组件

react怎么构建组件

作者: 哼_ | 来源:发表于2017-08-17 14:12 被阅读11次

首先是

ES5的写法:
const Com1 = React.createClass({
//创建一个类,就是构建一个组件 
  render(){
    return <h1>hello ES5 react component!!!</h1>
  }
});
ReactDOM.render(
    <div>
        <h1>Hello React!!!</h1>
        <Com1></Com1>
    </div>,
    document.querySelector(".box")
)
ES6 写法
class Com2 extends React.Component{
    render(){
        return  <h1>Hello ES6 React component</h1>
    }
};
ReactDOM.render(
    <div>
        <h1>Hello React!!!</h1>
        <Com1></Com1>
              {/*单双标签都可以,只要有结束字符就可以  <Com2></Com2>可以写成<Com2/>*/}
                <Com2></Com2>
    </div>,
    document.querySelector(".box")
)

我们在class Com2 里面写一个根节点

class Com2 extends React.Component{
    constructor(props){
        super(props);
    }
    render(){
        return <div>
<h1>Hello ES6 React component</h1>
<h3>{ this.props.msg }</h3>
{/*<Com1 msg="随便" items={["t4","t5","t6"]}></Com1>*/}
            </div>
            
    }
};
ReactDOM.render(
    <div>
        <h1>Hello React!!!</h1>
<Com1 msg="你好"/>
        <Com2 msg="你好" ></Com2>
    </div>,
    document.querySelector(".box")
)

在标签上传值

我们在 Com2 标签上进行传值,我们通过标签属性设置了一个msg,我们怎么拿到 msg 呢,就是 在class 里面,写一个标签把变量放在标签里面{ this.props.msg }
但是,ES6 继承下来的类,没有this,要显示的传,一个constructor(props){
super(props);}

const Com1 = React.createClass({
    getDefaultProps(){
        return {
            msg :"默认的msg"
        }
    },
    propTypes:{
        msg : React.PropTypes.oneOf(["a","b"])
    },
    render(){
        console.log(1,this.props)
        console.log(2,this.props.children)
        return <div>
            <h1>Hello React component ES5</h1>
            <span>{ this.props.msg }</span>
            <ul>
                {
                    this.props.items.map((item,i)=>{
                        return <li key={ i }>{ item }</li>
                    })
                }
            </ul>
            {this.props.children}
            {/*
                this.props.children.map((m,n)=>{
                    return <i key={n}>{ m }</i>
                })*/
            }
            {
                React.Children.map(this.props.children,(m,n)=>{
                    return <b key={n}>{m}</b>
                })
            }
        </div>
    }
});
ReactDOM.render(
    <div>
        <h1>Hello React!!!</h1>
        <Com1 msg="b" 
        items={ ["t1","t2","t3"] }>
        </Com1>

        <Com2 msgs="你好" ></Com2>
        <Com2 />
    </div>,
    document.querySelector(".box")
)

this.props

组件外向组建内传值,用this,props
传值的方法是一样的,在标签里面写一个属性,但是接值得时候方法不一样:由于继承的子类没有this(作用域),所以在ES6中,需要使用construtor 得到 this
// 而在ES5中,createClass 所创建的类,自动拥有this,可直接使用this.props
// this.props 得到父集向下传递的数据
// this.props.children 得到组件的原始内容(子元素)
//当有一个子元素时,返回对象
// 当有多个元素时,返回数组
// 没有子元素时,返回 undefined

一个组件,两个地方用,
把组件一,items 的t1,t2.t3.用this.props.items.map()的方法遍历出来,用 li 标签包裹
我们再在组件里面添一个属性,

相关文章

  • react怎么构建组件

    首先是 ES5的写法: ES6 写法 我们在class Com2 里面写一个根节点 在标签上传值 我们在 Com2...

  • 组件分享之前端组件——用于从 JSON Schema 构建 We

    组件分享之前端组件——用于从 JSON Schema 构建 Web 表单的 React 组件react-jsons...

  • React概念

    介绍 React的核心思想是:封装组件。React大体包含下面概念: 组件React 应用都是构建在组件之上。上面...

  • React入门(二)

    三、React组件 React 组件基本上是由组件的构建方式、组件内的状态属性与生命周期方法组成 React.cr...

  • React Native - 01 - Hello World!

    React Native就像React,但它使用本地组件而不是Web组件作为构建块。因此,要理解React Nat...

  • 深入浅出React和Redux学习笔记(二)

    设计高质量的React组件 构建高质量的React组件的原则和方法: 划分组件边界的原则; React组件的数据种...

  • 3.组件

    React 组件 可以这么说,一个 React 应用就是构建在 React 组件之上的。 组件有两个核心概念: p...

  • 3.组件

    React 组件 可以这么说,一个 React 应用就是构建在 React 组件之上的。 组件有两个核心概念: p...

  • React Native 分析(一)基本原理概述

    React 的理念React 的主要思想是通过构建可复用组件来构建用户界面。所谓组件,其实就是有限状态机(FSM)...

  • react+redux+router入门总结

    react+redux+router入门总结 目录 构建配置 React组件、css module React R...

网友评论

      本文标题:react怎么构建组件

      本文链接:https://www.haomeiwen.com/subject/kljwrxtx.html