我目前正在使用 flow 和 eslint 来清理我们的代码库。
Eslint 告诉你将无状态组件转换为无状态功能组件。但我还没有找到一个例子来说明他们的 props 如何正确输入以适应流程。这是我尝试过的:
原始组件:
type Props = {onPress : Function}
export default class MyButton extends React.Component <Props> {
render() {
... // use this.props.onPress
}
}
版本 1(我希望函数如何工作):
这里流程说 Cannot create MyButton element because a callable signature is missing in props [1] but exists in function type [2].
const MyButton = (onPress: Function) => (
return(
... // use onPress
)
}
版本 2(如 here 中所示):
这里 eslint 提示找不到 onPress
。 ${onPress}
也不起作用。
Flow 提示:缺少用于解构的类型注释。
const MyButton = ({onPress: Function}) => (
return(
... // use onPress (or ${onPress} ??)
)
}
那么我该如何解决呢?
最佳答案
你的版本 2 很接近,但你不能在解构中定义类型;您必须定义整个对象类型。看起来像这样
type Props = {
onPress: Function
}
const MyButton = ({ onPress }: Props) => (
return(
...
)
}
关于javascript - 在 React Native 中使用流为无状态功能组件定义 Prop 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49172736/