函数式无状态组件
const MyComponent=(props)=(<div>hello {props.name}</div>)//无状态组件的props
控制传入的属性
class HelloMessage extends React.Component { // 若是需要绑定 this.方法或是需要在 constructor 使用 props,定义 state,就需要 constructor。若是在其他方法(如 render)使用 this.props 则不用一定要定义 constructor constructor(props) { super(props);//构造函数使用props时用,否则可移除,有constructor必有super否则无this this.state = {}
this.函数=this.函数.bind(this)//自己绑定this的指向
} render() { return ( <div>Hello {this.props.name}</div> ) } } //验证类型 HelloMessage.propTypes = { name: React.PropTypes.string, } //预设默认值 HelloMessage.defaultProps = { name: 'Zuck', }
//设定默认值(2)
static defaultProps = {
checked: false,
maxLength: 10, }; // 注意有分号
// 验证类型(2) static propTypes = { checked: React.PropTypes.bool.isRequired, maxLength: React.PropTypes.number.isRequired };