Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React的prop-types #5

Open
Wscats opened this issue May 24, 2017 · 0 comments
Open

React的prop-types #5

Wscats opened this issue May 24, 2017 · 0 comments

Comments

@Wscats
Copy link
Owner

Wscats commented May 24, 2017

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'
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant