处理使用对象破坏和对象休息的组件的道具类型的正确方法是什么......传播以收集道具

jak*_*ies 2 javascript ecmascript-6 reactjs

我有一个像这样的组件:

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?
}
Run Code Online (Sandbox Code Playgroud)

Component解构它props,抓住active道具并将“其余部分”收集到rest. 有没有办法验证rest使用prop-types?是必需的吗?不知道该怎么办。

TKo*_*KoL 5

https://www.ian-thomas.net/custom-proptype-validation-with-react/

基本上,道具类型确实允许自定义验证。你把它设置为

Component.propTypes = {
  rest: function(props, propName, componentName) { // return null if all is well }
}
Run Code Online (Sandbox Code Playgroud)