【问题标题】:How do I inherit class with FlowType?如何使用 FlowType 继承类?
【发布时间】:2018-10-31 02:29:36
【问题描述】:

使用 React(ReactNative)。

想要继承 Component 类,但在这种情况下会出现流程错误的问题。

TryFlow

/* @flow */

import React, { Component } from 'react';
import { Text, View } from 'react-native';

type commonProps ={
  actions: Object,
};

type someProps = commonProps & {
  someNumber: number,
};

class CustomComponent extends Component<commonProps> {
  static defaultProps: {};
  props: commonProps;
}

class MyComponent extends CustomComponent {
  props: someProps;

  render() {
    return (
      <Text>{this.props.someNumber}</Text>
    );
  }
}

怎么办???

【问题讨论】:

    标签: javascript reactjs react-native flowtype


    【解决方案1】:

    我猜你应该为类设置类型注释。像这样:

    class CustomComponent<commonProps> extends Component<Props, State> {
       static defaultProps: {};
       props: commonProps;
    }
    

    【讨论】:

    • 谢谢。在这种情况下,将返回 Can not resolve nameTryFlow
    • 因为您需要为状态设置类型,如果您将拥有状态。试试看class CustomComponent extends Component&lt;commonProps&gt; {}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    相关资源
    最近更新 更多