Reputation: 4758
I wonder whether you could write the following code in a less verbose way:
class MyComponent extends Component {
static propTypes = {
foo: PropTypes.string.isRequired,
bar: PropTypes.string,
};
static defaultProps = {
bar: '',
};
Something like this:
class MyComponent extends Component {
static propTypes = {
foo: PropTypes.string.isRequired,
bar: [PropTypes.string, ''],
};
Or this:
class MyComponent extends Component {
static propTypes = {
foo: PropTypes.string.isRequired,
bar: PropTypes.string(''),
};
Upvotes: 1
Views: 710
Reputation: 51
No, with the current implementation you can't.
PropTypes are needed for development/test runtime purposes (https://facebook.github.io/react/blog/2017/04/07/react-v15.5.0.html#migrating-from-react.proptypes), default Values instead will be a part of the final build.
In the future react team plans to separate PropTypes from the rest of the React package.
You can propose this contacting the react team, if they are interested.
Upvotes: 1