最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Making propTypes mandatory and is there stateTypes - Stack Overflow

programmeradmin2浏览0评论

In React documentaton, the following has been mentioned under the heading propValidation:

React.createClass({
  propTypes: {
    // You can declare that a prop is a specific JS primitive. By default, these
    // are all optional.
    optionalArray: React.PropTypes.array,
    optionalBool: React.PropTypes.bool,

.....

It is mentioned in the ment that "these are all optional". How can I make it mandatory for that ponent ?

Also, similar to propTypes is there stateTypes which corresponds to this.state ?

In React documentaton, the following has been mentioned under the heading propValidation:

React.createClass({
  propTypes: {
    // You can declare that a prop is a specific JS primitive. By default, these
    // are all optional.
    optionalArray: React.PropTypes.array,
    optionalBool: React.PropTypes.bool,

.....

It is mentioned in the ment that "these are all optional". How can I make it mandatory for that ponent ?

Also, similar to propTypes is there stateTypes which corresponds to this.state ?

Share asked Sep 10, 2015 at 17:40 SibiSibi 48.8k18 gold badges104 silver badges171 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

You can declare required props as follows:

React.createClass({
  propTypes: {
    myPropStringRequired: React.PropTypes.string.isRequired,
    myPropArrayRequired: React.PropTypes.array.isRequired,
    myPropBooleanRequired: React.PropTypes.bool.isRequired,
...

As per the second question about stateTypes, please check this question.

发布评论

评论列表(0)

  1. 暂无评论