ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

React16入门2

2021-12-01 15:58:32  阅读:36  来源: 互联网

标签:function return 入门 render element props 组件 React16


组件的用法

<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>

<script src="https://unpkg.com/react/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>


<script type="text/babel">
    //定义组件-函数写法
    function Component1() {
        return <h1>I am Component1.</h1>;
    }
    //定义组件-ES6写法
    class Component2 extends React.Component {
        render() {
            return <h1>I am Component2.</h1>;
        }
    }
    //组件数组
    const element = [<Component1 /> , <Component2 />];
    
    ReactDOM.render(
        element,
        document.getElementById('content')
    );
</script>

<div id="content"></div>


复合组件

<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>

<script src="https://unpkg.com/react/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>


<script type="text/babel">

    function Name(props) {
        return <h1>名称:{props.name}</h1>;
    }
    class Sex extends React.Component {
        render() {
            return <h1>性别:{this.props.sex}</h1>;
        }
    }
    //复合新组件
    function Person() {
        return (
            <div>
                <Name name="小明" />
                <Sex sex="男" />
            </div>
        );
    }
    
    const element = <Person />;
    
    ReactDOM.render(
        element,
        document.getElementById('content')
    );
</script>

<div id="content"></div>


标签:function,return,入门,render,element,props,组件,React16
来源: https://blog.csdn.net/buyueliuying/article/details/121656871

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有