【问题标题】:Are there any flow types definition for react-native styles?react-native 样式是否有任何流类型定义?
【发布时间】:2017-06-06 09:22:29
【问题描述】:

我正在制作一个 react native 组件并使用 Flow 来定义这样的属性类型:

type Props = {
    text: string,
    textStyle: object
}

然后将这些属性传递给我的组件内的 Text 组件。 react-native 是否有任何流类型定义可以让我做这样的事情:

textStyle: TextStyle

现在 Flow 将能够检查 textStyle 值是否仅包含 Text 组件样式允许的键。

我看到 TypeScript (React.TextStyle) 有接口定义,但找不到 Flow 的任何东西。

【问题讨论】:

    标签: react-native flowtype


    【解决方案1】:

    更新 3:

    目前的最佳做法是:

    import type {
       ViewStyleProp,
       TextStyleProp,
       ImageStyleProp,
    } from 'react-native/Libraries/StyleSheet/StyleSheet';
    

    更新 2: 以下PR 使事情变得更简单,截至今天流类型样式如下。

    type StyleValue = {[key: string]: Object} | number | false | null;
    type StyleProp = StyleValue | Array<StyleValue>; 
    

    已过时:following PR 修复了它,可以从 v0.52.0 中找到

    可以做到以下几点:

    // @flow
    
    import type { StyleObj } from 'react-native/Libraries/StyleSheet/StyleSheetTypes';
    
    type Props = {
        style?: StyleObj
    };
    

    以下讨论:https://github.com/flowtype/flow-typed/issues/631#issuecomment-282009393

    【讨论】:

    • 不应该是{[key: string]: Object | number | string | null}吗?
    • @AMB 下面的答案是最新的(截至 2018 年 9 月 13 日)。
    【解决方案2】:

    更新

    StyleObj 不再是 StyleSheetTypes 的一部分。

    现在导入:

    {LayoutStyle, TransformStyle, ShadowStyle, ViewStyle, TextStyle, ImageStyle, DangerouslyImpreciseStyle} from 'react-native/Libraries/StyleSheet/StyleSheetTypes';

    在你的情况下,你应该:

    import { TextStyle } from 'react-native/Libraries/StyleSheet/StyleSheetTypes';

    【讨论】:

    • 听从您的建议后,我看到了ViewStyle Application of polymorphic type needs &lt;list of 0-2 arguments&gt;. (Can use `*` for inferrable ones)编辑通过导入ViewStyleProp而不是ViewStyle解决它!
    • import type { ViewStyleProp } from "react-native/Libraries/StyleSheet/StyleSheet";
    猜你喜欢
    • 2016-04-05
    • 2019-05-07
    • 2021-06-30
    • 2016-09-25
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    相关资源
    最近更新 更多