我有一个组件接收错误作为字符串或具有2个必需属性的对象.但是也可以为此道具传递null.在我当前的设置中,当传递null时,React会发出警告:
警告:失败的道具类型:
error提供的 无效道具ErrorDialog
我应该为React改变什么来允许null | 字符串| 这个形状的物体?谢谢!
ErrorDialog.propTypes = {
onResetError: PropTypes.func.isRequired,
error: PropTypes.oneOfType([
PropTypes.shape({
id: PropTypes.number.isRequired,
defaultMessage: PropTypes.string.isRequired,
}),
PropTypes.string,
]),
};
Run Code Online (Sandbox Code Playgroud)
完整的代码是:
import React, { PropTypes } from 'react';
import Dialog from 'material-ui/Dialog';
import FlatButton from 'material-ui/FlatButton';
import { FormattedMessage } from 'react-intl';
const ErrorDialog = ({ error, onResetError }) => {
function renderError() {
if (!error) {
return null;
} else if (typeof error === 'string') …Run Code Online (Sandbox Code Playgroud)