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

Use props validation everywhere #6

Open
slorber opened this issue Jan 24, 2014 · 0 comments
Open

Use props validation everywhere #6

slorber opened this issue Jan 24, 2014 · 0 comments

Comments

@slorber
Copy link
Contributor

slorber commented Jan 24, 2014

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

bblfish added a commit that referenced this issue Jun 5, 2017
bblfish added a commit that referenced this issue Jun 5, 2017
bblfish added a commit that referenced this issue Jun 5, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant