I have a component that accepts an error as a string or object with 2 required properties. But null can also be passed for this support. In my current installation, React throws a warning when passing null :
Warning: Faulty support type: Invalid prop error specified in ErrorDialog
What should I change for React to allow null | string | an object with this shape ? Thanks!
ErrorDialog.propTypes = { onResetError: PropTypes.func.isRequired, error: PropTypes.oneOfType([ PropTypes.shape({ id: PropTypes.number.isRequired, defaultMessage: PropTypes.string.isRequired, }), PropTypes.string, ]), };
Full code:
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') { return error; } else if (typeof error === 'object') { return <FormattedMessage {...error} />; } return null; } return ( <Dialog modal={false} open={Boolean(error)} actions={ <FlatButton label="OK" primary onTouchTap={onResetError} /> } > {renderError()} </Dialog> ); }; ErrorDialog.propTypes = { onResetError: PropTypes.func.isRequired, error: PropTypes.oneOfType([ PropTypes.shape({ id: PropTypes.number.isRequired, defaultMessage: PropTypes.string.isRequired, }), PropTypes.string, ]), }; export default ErrorDialog;
I want to hide the dialog when there is no error, show the source line if the error has a type string and displays the translated message if the message descriptor is specified.
UPDATE: I went with this solution:
ErrorDialog.propTypes = { onResetError: PropTypes.func.isRequired,
javascript reactjs
Max semikin
source share