有没有办法使用React.PropTypes
. 特别想混合自定义验证和库存验证。
我有两个道具,一个 objectoptions
和一个 string value
。我想检查它props.value
是一个字符串,也是对象上的一个键。使用 coffeescript 这看起来像:
propTypes:
options: React.PropTypes.Object.isRequired
value: (props, propName, componentName) ->
unless props[propName] of props.options
new Error('my custom message')
这很好用,但我也想确保我的值是字符串类型。我确信我可以在自定义函数中手动坚持该验证没有问题,但理想情况下,我只想使用React.PropTypes.string.isRequired
. 我试着把它放在自定义函数中并执行它,但没有奏效。以下也不起作用:
value: React.PropTypes.string.isRequired && (props, propName, componentName) ->
unless props[propName] of props.options
new Error('my custom message')
有没有办法使用内置验证器的反应来完成这项工作,或者在我的函数中重写它是唯一的选择?