jeffcarp / frontend-hyperpolyglot

The same operations in React, Angular 1&2, Ember, Polymer, Vue, and Riot
http://jeffcarp.github.io/frontend-hyperpolyglot
MIT License
269 stars 22 forks source link

`shape` and custom PropType validation for react #13

Open wmonk opened 8 years ago

wmonk commented 8 years ago

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.