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?是必需的吗?不知道该怎么办。
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)
| 归档时间: |
|
| 查看次数: |
2019 次 |
| 最近记录: |