Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Use props validation everywhere #6

@slorber

Description

@slorber

http://facebook.github.io/react/docs/reusable-components.html

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,
    optionalFunc: React.PropTypes.func,
    optionalNumber: React.PropTypes.number,
    optionalObject: React.PropTypes.object,
    optionalString: React.PropTypes.string,

    // You can ensure that your prop is limited to specific values by treating
    // it as an enum.
    optionalEnum: React.PropTypes.oneOf(['News','Photos']),

    // You can also declare that a prop is an instance of a class. This uses
    // JS's instanceof operator.
    someClass: React.PropTypes.instanceOf(SomeClass),

    // You can chain any of the above with isRequired to make sure an error is
    // thrown if the prop isn't provided.
    requiredFunc: React.PropTypes.func.isRequired

    // You can also specify a custom validator.
    customProp: function(props, propName, componentName) {
      if (!/matchme/.test(props[propName])) {
        throw new Error('Validation failed!')
      }
    }
  },
  /* ... */
});

Using props validation ensure that each component is used correctly, through a defined interface.
It's like assertions for a component done in a declarative way.

This should be used almost everywhere to help detect the consistency issues that could happen when the application grows.

We need to fail-fast to detect problems

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions