react-tutorial icon indicating copy to clipboard operation
react-tutorial copied to clipboard

React的prop-types

Open Wscats opened this issue 8 years ago • 0 comments

image 新版本的react的prop-type已经单独成包了,所以要先安装prop-types模块

npm install prop-types --save-dev

image 然后在组件中引入PropTypes模块

import React from 'react';
import PropTypes from 'prop-types';
class Skill extends React.Component {
//code
constructor(props) {
        super(props);
        // 设置 initial state
        this.state = {
            text: props.initialValue || 'Hello Wscats'
        };
    }
}

注意这里以前是React.PropTypes,现在为PropTypes

Skill.propTypes = {
	//定义传入props中的属性各种类型
    initialValue: PropTypes.string.isRequired//PropTypes.string
};
Skill.defaultProps = {
	//组件默认的props对象
    initialValue: 'Hello Oaoafly'
};

Wscats avatar May 24 '17 02:05 Wscats