简体   繁体   English

如何声明对应于可为空数字的 PropType?

[英]How can I declare a PropType corresponding to a nullable number?

I'm looking for a PropType that means我正在寻找一个PropType这意味着

"this is required, and it will either be a number or be null" “这是必需的,它将是一个数字或为空”

In other words, what I have now is换句话说,我现在拥有的是

PropTypes.number.isRequired

but that throws a warning if a null value gets passed in, yet I want null to be an acceptable value.但是如果传入null值,则会引发警告,但我希望null值是可接受的值。

Just use:只需使用:

PropTypes.number

By default all prop types aren't required (ie allow null or undefined ) unless you pop a .isRequired on the end of them.默认情况下,所有 prop 类型都不是必需的(即允许nullundefined ),除非您在它们的末尾弹出.isRequired

You can see the full docs for proptypes here:您可以在此处查看 proptypes 的完整文档:

You simply could use:你可以简单地使用:

PropTypes.number

and in defaultProps:在 defaultProps 中:

yourPropName: null

Currently prop-types library don't allow this.目前prop-types库不允许这样做。 The way i get around this is using a custom validation function我解决这个问题的方法是使用自定义验证功能

 MyComponent.propTypes = { nullableArray: function(props, propName, componentName) { const { propName: data } = props; if (data === undefined) { return new Error(`Undefined ${propName} is not allowed`); } if (data !== null) { return; //this is allow when data is loading } if (!_.isArray(data)) { return new Error(`${propName} must be an array`); } } };

You can make another step further to create a high order function to generate the validation function.您可以进一步创建一个高阶函数来生成验证函数。 this should get you started这应该让你开始

 generateRequiredOrNullValidationFunction = expectedType => { if (expectedType !== "array") { return Error(`Unexpected ${expectedType}`); } return function(props, propName, componentName) { const { [propName]: data } = props; if (data === undefined) { return new Error(`Undefined ${propName} is not allowed`); } if (data !== null) { return; //this is allow when data is loading } if (expectedType === "array" && !_.isArray(data)) { return new Error(`${propName} must be an array`); } }; };

In this snippet, expectedType is a enumeration such as bool , array , number ...在这个片段中, expectedType是一个枚举,例如boolarraynumber ...

To help with this quite common issue, I created a clean and fully-typed wrapper called better-prop-types :为了帮助解决这个非常常见的问题,我创建了一个干净且类型完整的包装器,称为Better-prop-types

import BetterPropTypes from 'better-prop-types'

// ...

MyComponent.propTypes = {
  aProp: BetterPropTypes.string.isRequiredButNullable,
}
import propTypes from 'prop-types';

const nullable = propType => (props, propName, ...rest) =>
  props[propName] === null ? null : propType(props, propName, ...rest);

const testMe = {
  a: 'string',
  b: 420,
  c: null,
  d: undefined,
  e: undefined
};

const testSchema = {
  a: nullable(propTypes.string.isRequired),
  b: nullable(propTypes.string.isRequired),
  c: nullable(propTypes.number.isRequired),
  d: nullable(propTypes.bool.isRequired),
  e: nullable(propTypes.number)
};

propTypes.checkPropTypes(testSchema, testMe, 'prop', 'testMe');
// Warning: Failed prop type: Invalid prop `b` of type `number` supplied to `testMe`, expected `string`.
// Warning: Failed prop type: The prop `d` is marked as required in `testMe`, but its value is `undefined`.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM