最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React warns about passed prop with value null, where the PropType for the prop is not required - Stack Overflow

programmeradmin3浏览0评论

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 to ErrorDialog

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 to ErrorDialog

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
Add a ment  | 

1 Answer 1

Reset to default 7

Read 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.'
    );
  }
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论