如何声明与可为空数字对应的 PropType? [英] How can I declare a PropType corresponding to a nullable number?

查看:49
本文介绍了如何声明与可为空数字对应的 PropType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个 PropType 这意味着

I'm looking for a PropType that means

这是必需的,它要么是一个数字要么是空的"

"this is required, and it will either be a number or be null"

换句话说,我现在拥有的是

In other words, what I have now is

PropTypes.number.isRequired

但是如果 null 值被传入,则会引发警告,但我希望 null 是一个可接受的值.

but that throws a warning if a null value gets passed in, yet I want null to be an acceptable value.

推荐答案

只需使用:

PropTypes.number

默认情况下不需要所有的 prop 类型(即允许 null undefined),除非你弹出一个 .isRequired 在他们的末尾.

By default all prop types aren't required (i.e. allow null or undefined) unless you pop a .isRequired on the end of them.

您可以在此处查看 proptype 的完整文档:

You can see the full docs for proptypes here:

这篇关于如何声明与可为空数字对应的 PropType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆