React中使用static defaultProps作為默認值
在子組件中傳入標簽后在子組件內部使用 {this.props.children}接收
import React, { Component } from 'react'
class Child extends Component{
static defaultProps = {
title:'沒傳過來時候顯示的標題'
}
constructor(props){
super(props)
this.state={
}
}
render() {
return (
<div>
{this.props.children} {/* 通過this.props.children取得父組件傳過來的全部節(jié)點 */}
<h1>{this.props.title}</h1>
</div>
)
}
}
export default class app6 extends Component {
constructor(props){
super(props)
this.state={
title:'要傳過來的標題'
}
}
render() {
return (
<div>
<Child title={this.state.title}>
<div>
<p>有一個p</p>
<button>按鈕</button>
</div>
</Child>
</div>
)
}
}