1
我不知道你是否能在一个更简洁的方式写出下面的代码:你可以将propTypes与defaultProps结合吗?
class MyComponent extends Component {
static propTypes = {
foo: PropTypes.string.isRequired,
bar: PropTypes.string,
};
static defaultProps = {
bar: '',
};
事情是这样的:
class MyComponent extends Component {
static propTypes = {
foo: PropTypes.string.isRequired,
bar: [PropTypes.string, ''],
};
或者这样:
class MyComponent extends Component {
static propTypes = {
foo: PropTypes.string.isRequired,
bar: PropTypes.string(''),
};
你为什么要结合两者?两者都有自己的不同用例.. –
React文档中描述了所有可用的选项,因此如果没有在那里描述它,并且您希望建议新的语法,我建议您联系反应团队。 – ivarni