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

标签 javascript reactjs ecmascript-6

我有这样一个组件:

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/

相关文章:

javascript - 如何使用正则表达式来表示某些特定的国家/地区字符?

javascript - 下拉产品,将价格输入文本字段

javascript - 巴别塔 : Function parameter types in ES6

javascript - Window Active Directory React JS 自动身份验证

javascript - JS - 创建新 worker 导致 404

javascript - 将无状态组件拆分为更小的部分

javascript - 如何更新嵌套数组(多重 react 选择)

javascript - JavaScript 可以监听网页中嵌入的 QuickTime 对象的状态变化吗?

javascript - 如何在 react 应用程序和将其作为静态站点托管的快速 js 服务器之间共享环境变量

javascript - 在 promise 中使函数超时的最佳一般做法是什么