【问题标题】:How to combine TouchableOpacity Props type with additional fields如何将 TouchableOpacity Props 类型与附加字段相结合
【发布时间】:2018-11-29 17:14:46
【问题描述】:

我正在尝试将 color 道具添加到 styled-components 包装的 TouchableOpacity 并让流程正确输入。

type TouchableOpacityProps = $PropertyType<Element<TouchableOpacity>, "props">;
type ButtonTouchableProps = { color: string } & TouchableOpacityProps;

const ButtonTouchable: ComponentType<ButtonTouchableProps> = styled.TouchableOpacity`
  background-color: ${props => props.color};
`;

但是,我在使用 &lt;ButtonTouchable color="#CCCCCC" /&gt; 时收到此流量警告:

无法创建 ButtonTouchable 元素,因为属性 color 是 在对象类型 [1] 中缺失,但存在于道具 [2] 中。 (参考文献:[1] [2])

【问题讨论】:

    标签: react-native flowtype


    【解决方案1】:
    // @flow
    import { type ElementConfig, type ComponentType } from 'react';
    import { TouchableOpacity } from 'react-native';
    
    type ButtonTouchableProps = {|
      ...$Exact<ElementConfig<typeof TouchableOpacity>>,
      color: string,
    |};
    
    const ButtonTouchable: ComponentType<ButtonTouchableProps> = styled.TouchableOpacity`
      background-color: ${props => props.color};
    `;
    

    【讨论】:

    • ElementConfig 是我所缺少的。谢谢!
    猜你喜欢
    • 2021-08-09
    • 2020-04-07
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多