我有这样一个组件:
import React from 'react'
import { bool } from 'prop-types'
const Component = ({ active, ...rest}) => (
// ...does things
)
Component.propTypes = {
active: bool.isRequired,
// -> how do i handle { ...rest } here?
rest: object // ? works, but is it the right solution?
}
Component
解构其 props
,获取 active
prop 并将“rest”收集到 rest
中。有没有一种方法可以使用 prop-types
来验证 rest
?是必需的吗?不确定该怎么做。
最佳答案
https://www.ian-thomas.net/custom-proptype-validation-with-react/
基本上,prop-types 允许自定义验证。你把它设置为
Component.propTypes = {
rest: function(props, propName, componentName) { // return null if all is well }
}
关于javascript - 处理使用对象破坏和对象休息的组件的 Prop 类型的正确方法是什么......传播收集 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49198025/