frontend-hyperpolyglot icon indicating copy to clipboard operation
frontend-hyperpolyglot copied to clipboard

`shape` and custom PropType validation for react

Open wmonk opened this issue 9 years ago • 0 comments

Shape

React will allow you to specify the shape of an object prop, this is an example:

var Comp = React.createClass({
  propTypes: {
    name: React.PropTypes.shape({
      first: React.PropTypes.string.isRequired,
      last: React.PropTypes.string
    }).isRequired
  },
  render() {
    return <div>Hello</div>;
  }
});

ReactDOM.render(<Comp name={{ first: "James" }}/>, document.querySelector('#root'));

I'm not sure if this is something allowed by other frameworks, but it's a pretty good feature.

Custom PropType Validation

React offers functionality to add a custom prop validator, which again is a good feature, for example you might want to do hex code validation. Again, not sure if this is something unique to React, but it's a nice feature.

wmonk avatar Jun 03 '16 10:06 wmonk