react中类似vue的插槽this.props.children的用法


父组件

 <TestHanderClick bg="blue">
        <p> 如果我要显示的话,父组件是双标签,子组件中有this.props.children</p>
        <div>类似于匿名插槽</div>
  </TestHanderClick>

子组件

import React, { Component } from "react";
import "./base.css"


// 父组件
export class TestHanderClick extends Component {
  //  static defaultProps是默认的写法,人家规定这样写的,你的默认值
  static defaultProps={
    bg:"pink",
    wi:"400px",
    he:"200px"
  }

  render() {
    return (
      // 使用值
      <div style={{  background: this.props.bg, width: this.props.wi,  height:this.props.he  }}>
        123
        {this.props.children}
         //通过这个组件,可以获取父组件中的数据
      </div>
    )
  }
}

export default TestHanderClick;


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM