当前位置 : 主页 > 网页制作 > React >

React Component --React

来源:互联网 收集:自由互联 发布时间:2021-06-15
一、Use functions to define components script type="text/babel" function HelloWord() { return h1Hello World!/h1; }/script 二、Use ES6 class to define components script type="text/babel" class HelloWord extends React.Component { render()

一、Use functions to define components

<script type="text/babel">
    function HelloWord() {
        return <h1>Hello World!</h1>;
    }
</script>

二、Use ES6 class to define components

<script type="text/babel">
    class HelloWord extends React.Component {
        render() {
            return <h1>Hello World!</h1>;
        }
    }
</script>

三、Transfer parameters to functions

<script type="text/babel">
    function HelloWorld(props) {
        return <h1>Hello {props.name}!</h1>;
    }
    const element = <HelloWorld name="Word"/>;

    ReactDOM.render(
        element,
        document.getElementById('example')
    );
</script>

四、Composite components

<script type="text/babel">
    function WebsiteName(props) {
        return <div>网站名称:{props.name}</div>;
    }

    function WebsiteUrl(props) {
        return <div>网站地址:{props.url}</div>;
    }

    function Application() {
        return (
            <div>
                <WebsiteName name="蓝色旗帜"/>
                <WebsiteUrl url="http://www.blueflags.cn"/>
            </div>
        );
    }

    ReactDOM.render(
        <Application/>,
        document.getElementById('example')
    );
</script>

参考:https://www.runoob.com/react/react-components.html

网友评论