import React, { Component } from 'react'
import PropTypes from 'prop-types'
// 父传子
class Student extends Component {
constructor(props) {
super(props)
}
static propTypes = {
// 限制传入的数据类型为string
msg: PropTypes.string
// 限制必须传值
// msg: PropTypes.string.isRequired
}
// 定义默认的值
static defaultProps = {
msg : "默认值"
}
render() {
return (
<h1>{this.props.msg}</h1>
)
}
}
export default class App9 extends Component {
state = {
msg : "传家宝"
}
render() {
return (
<div>
<Student msg={this.state.msg}></Student>
</div>
)
}
}
React:父传子
于 2022-02-28 11:01:09 首次发布