Skip to content

React的prop-types #5

Open
Open
@Wscats

Description

@Wscats

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'
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions