react入门-props.children

在ReactDOM.render里面我们写我们的自定义组件的时候有时需要加一下子元素进去:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id="app"></div>
    <script src="common/react.js"></script>
    <script src="common/react-dom.js"></script>
    <script src="https://cdn.bootcss.com/babel-core/5.8.38/browser.js"></script>
    <script type="text/babel">
       class App extends React.Component {
           render() {
               return <div>
                    {/*props.children获取下面4个子节点:3个元素节点和1个文字节点*/}
                    {this.props.children.map((item,index)=> <p key={index}>{item}</p>)}
               </div>
           }
       }
       ReactDOM.render(
           <App>
                {/*加一些子元素*/}
                <span>rick</span>
                <span>zhangamie</span>
                <span>react</span>
                2132132
           </App>,
           document.getElementById('app')
       )
    </script>
</body>
</html>

 

posted @ 2018-03-04 17:56  张啊咩  阅读(2401)  评论(0编辑  收藏  举报