【问题标题】:Using flow with react-native使用带有 react-native 的流
【发布时间】:2019-07-19 12:34:35
【问题描述】:

我是 flow 新手,我没有成功将它与 react-native 一起正确使用。我不明白我应该如何加载 RN 类型。 例如,我正在尝试键入一个返回 nativeEvent.layout 的函数,该函数的类型为 LayoutRectangle

我试过了:import * as RN from 'react-native';

但按原样使用它会给我Cannot resolve name LayoutRectangleRN.LayoutRectangle 给我:Cannot get RN.LayoutRectangle because property LayoutRectangle is missing in module react-native [1]

这是我正在尝试制作的示例组件。

// @flow
import * as React from 'react';
import * as RN from 'react-native';

type CompReference = {
  getLocation?: () => LayoutRectangle,
}
type Ref<T> = {
  current: null | T
}
type Props = {
  value: string
}

function CompFunction({value = ''}: Props = {}, ref: CompReference & Ref<HTMLElement> = {}) {
    const location: LayoutRectangle = React.useRef(null);
    ref.getLocation = () => location.current || { height: 0, width: 0, x: 0, y: 0 };
    return (<RN.Text onLayout={({nativeEvent}) => location.current = nativeEvent.layout}>{value}</RN.Text>);
}

const Comp = React.forwardRef<Props, CompReference & Ref<HTMLElement>>(CompFunction);
Comp.displayName = 'Comp';

export default Comp;

我也收到有关 forwardRef 的错误:

Cannot call `React.forwardRef` with `CompFunction` bound to `render` because property `current` is missing in function type [1] but exists in `Ref` [2] in the second argument. (index.js:34:76)flow
Cannot call `React.forwardRef` with `LetterFunction` bound to `render` because property `getLocation` is missing in object type [1] but exists in `CompReference` [2] in the second argument. (index.js:34:76)flow
Cannot call `React.forwardRef` with `CompFunction` bound to `render` because property `getLocation` is missing in statics of function type [1] but exists in `CompReference` [2] in the second argument. (index.js:34:76)flow
Cannot call `React.forwardRef` with `CompFunction` bound to `render` because property `current` is missing in `HTMLElement` [1] but exists in `Ref` [2] in property `current` of the second argument. (index.js:34:76)flow
Cannot call `React.forwardRef` with `CompFunction` bound to `render` because in property `current` of the second argument: Either `CompReference` [1] is incompatible with `HTMLElement` [2]. Or `Ref` [3] is incompatible with `HTMLElement` [2]. (index.js:34:76)flow

我做错了什么?

【问题讨论】:

    标签: reactjs react-native flowtype flow-typed


    【解决方案1】:

    使用 Flow 时,需要导入类型。

    所以你需要添加类似的东西

    // @flow
    import * as React from 'react';
    import * as RN from 'react-native';
    import type {LayoutRectangle} from 'CoreEventTypes'; //Or wherever your type LayoutRectangle is defined
    

    请注意,我可能没有与您相同的 RN 版本,因为在我没有 current 属性的版本中,nativeEvent.layout 的类型为 Layout

    注意:如果类型没有导出到任何地方,您可以使用:

    import typeof {LayoutRectangle} from 'react-native';
    

    来源:https://flow.org/en/docs/types/modules/#importing-and-exporting-types-

    【讨论】:

    • 如果您仔细观察,nativeEvent.layout 在我的代码中不需要current 字段。这只是useRefReact 钩子的使用可能会给人这种感觉。
    • 什么是“CoreEventTypes”?此外,我不明白,因为当我在 VSCode 中悬停nativeEvent.layout 时,流程告诉我它的类型为RN.LayoutRectangle。那么它是从哪里得到这些信息的,为什么它无法在文件中访问呢?
    • @Sharcoux 您的文件中必须有一个export type LayoutRectangle,并且您必须导入类型。 VSCode 正确找到类型,但要在文件中使用 Flow,您需要导入您正在使用的类型。我编辑了我的答案。
    • 好的,我可以用import type { Layout } from 'react-native/Libraries/Types/CoreEventTypes'; 来做。我不知道这个LayoutRectangle 来自哪里。不过,由于 forwardRef 的错误,我仍然无法使用 flow...
    • 您还需要导入类型HTMLElementLetterReference,也许这会解决您的问题。顺便说一句,不需要创建自己的Ref 类型,只需使用 Maybe 类型:flow.org/en/docs/react/refs
    猜你喜欢
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    • 1970-01-01
    相关资源
    最近更新 更多