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