容器类组件和props.children

有一类组件,充当了容器的作用,它定义了一种外层结构形式,然后你可以往里面塞任意的内容。

因此我们可以使用容器组件完成布局,然后再通过props.children往里面填内容

eg. 使用Layout组件布局一个大盒子,里面装有左右两个盒子,再通过props.children[0]和props.children[1]填充左右两个盒子

/布局组件Layout

const Layout = (props) => {
    return (
        <div className="box">
            <div className="left">
                {props.children[0]}
            </div>
            <div className="right">
                {
                    props.children[1]
                }
            </div>
        </div>
    )

}


//样式
.box {
    margin: 100px;
}

.left {
    float: left;
    width: 200px;
    background-color: blue;
}

.right {
    float: left;
    width: 200px;
    background-color: red;
}
/传内容的组件App.js

const App = () => {
    return (
        <Layout>
            <div>hahahahah</div>
            <div>heiheiheiheiheiheihei</div>
        </Layout>
    )
}
/通过渲染容器组件来渲染内容index.js

ReactDOM.render(<App />, document.getElementById('root'))

效果展示:

总结 :容器组件Layout方便我们服用某种布局,相当于把一种布局封装起来!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值