【问题标题】:In react how can I use flow to type check only certain props, but ignore ANY others?在反应中,我如何使用 flow 只检查某些道具,而忽略其他道具?
【发布时间】:2018-01-13 01:07:16
【问题描述】:

我正在创建一些按钮组件,并且我有一些我需要的自定义道具,并希望通过流程检查它们。但由于它们是按钮,我还想要 HTML 按钮元素中的任何其他道具,但不想全部检查它们。

有什么方法可以做出反应,或者使用 npm 包让我输入检查我的新自定义道具并让组件接收任何其他道具?或者可能只限于 HTML 定义的?

【问题讨论】:

  • 可以用$FlowFixMe忽略

标签: reactjs flowtype


【解决方案1】:

您应该能够将其余的道具向下传递,而无需为其添加类型注释。

例子:

import React, { type Node } from 'react'

type Props = {
  primary?: boolean,
  children: Node
}

function Button({ primary, children, ...props }: Props) {
  return (
    <button 
      className={primary ? 'is-primary' : ''}
      {...props}
    >
      {children}
    </button>
  )
}

export default Button

用法:

function App() {
  return (
    <div>
      <Button primary onClick={() => console.log('clicked!')}>
        Click Me
      </Button>  
    </div>
  )
}

您也可以在flow.org/try 上查看。

【讨论】:

  • 谢谢。也用于流程/尝试,不知道这个!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-11
  • 2019-08-04
  • 2021-05-06
  • 2020-08-09
  • 2019-10-24
  • 2017-03-24
  • 1970-01-01
相关资源
最近更新 更多