I have a ponent that receives error as either a string or an object with 2 required properties. But null can also be passed for this prop. In my current setup React throws a warning when null is passed:
Warning: Failed prop type: Invalid prop
error
supplied toErrorDialog
What shall I change for React to allow null | string | object with this shape? Thank you!
ErrorDialog.propTypes = {
onResetError: PropTypes.func.isRequired,
error: PropTypes.oneOfType([
PropTypes.shape({
id: PropTypes.number.isRequired,
defaultMessage: PropTypes.string.isRequired,
}),
PropTypes.string,
]),
};
The full code is:
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 original string, if the error is of type string and render a translated message if a message descriptor is specified.
UPDATE: I went with the solution like this:
ErrorDialog.propTypes = {
onResetError: PropTypes.func.isRequired,
// eslint-disable-next-line consistent-return
error(props, propName, ponentName) {
const prop = props[propName];
if (prop !== null &&
typeof prop !== 'string' &&
!(typeof prop === 'object' && prop.id && prop.defaultMessage)) {
return new Error(
`Invalid prop \`${propName}\` supplied to ${ponentName}. Validation failed.`
);
}
},
};
I have a ponent that receives error as either a string or an object with 2 required properties. But null can also be passed for this prop. In my current setup React throws a warning when null is passed:
Warning: Failed prop type: Invalid prop
error
supplied toErrorDialog
What shall I change for React to allow null | string | object with this shape? Thank you!
ErrorDialog.propTypes = {
onResetError: PropTypes.func.isRequired,
error: PropTypes.oneOfType([
PropTypes.shape({
id: PropTypes.number.isRequired,
defaultMessage: PropTypes.string.isRequired,
}),
PropTypes.string,
]),
};
The full code is:
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 original string, if the error is of type string and render a translated message if a message descriptor is specified.
UPDATE: I went with the solution like this:
ErrorDialog.propTypes = {
onResetError: PropTypes.func.isRequired,
// eslint-disable-next-line consistent-return
error(props, propName, ponentName) {
const prop = props[propName];
if (prop !== null &&
typeof prop !== 'string' &&
!(typeof prop === 'object' && prop.id && prop.defaultMessage)) {
return new Error(
`Invalid prop \`${propName}\` supplied to ${ponentName}. Validation failed.`
);
}
},
};
Share
Improve this question
edited Aug 29, 2016 at 19:57
Max Semikin
asked Aug 29, 2016 at 17:14
Max SemikinMax Semikin
9741 gold badge11 silver badges20 bronze badges
2
- can you share your code, so that we can check? – Md.Estiak Ahmmed Commented Aug 29, 2016 at 17:18
- @Md.EstiakAhmmed added the full code. – Max Semikin Commented Aug 29, 2016 at 17:30
1 Answer
Reset to default 7Read this issue and this issue for discussions happened in the past. Just make props.error
optional and check the value in your code. Otherwise, you would need to implement your own custom prop validation.
From the docs:
// You can also specify a custom validator. It should return an Error
// object if the validation fails. Don't `console.warn` or throw, as this
// won't work inside `oneOfType`.
customProp: function(props, propName, ponentName) {
if (!/matchme/.test(props[propName])) {
return new Error(
'Invalid prop `' + propName + '` supplied to' +
' `' + ponentName + '`. Validation failed.'
);
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744327050a4568700.html
评论列表(0条)