import React,{ Component } from 'react';
//PropTypes 从react中分离出来,成为一个独立的包。
import PropTypes from 'prop-types';
export default class Formate extends Component {
constructor(props) {
super(props);
this.state = {
}
}
formateName(firstName,lastName) {
this.setState({
userName: firstName + '' + lastName
})
return firstName + '' + lastName;
}
render() {
//console.log(this.props.firstName,'',this.props.lastName)
return(
<div onClick={ ()=> { this.formateName(this.props.firstName,this.props.lastName) }}>
Formatesss {this.state.userName}
</div>
)
}
}
Formate.defaultProps = {
firstName: 'tianxia',
lastName: 'xiaoxiao',
}
//在React v15.5.0中,prop-types是react依赖的package;在React项目的master branch上,prop-types包已经和React无关了。
Formate.propTypes = {
firstName: PropTypes.string,
lastName: PropTypes.string
}
[参考文章]
(https://zhuanlan.zhihu.com/p/26286646?utm_source=tuicool&utm_medium=referral)