收到React流错误:模块不是多态类型。如何扩展课程?

时间:2018-12-19 12:03:41

标签: javascript reactjs flowtype javascript-inheritance

我正在尝试扩展现有布局以基本上覆盖方法renderHeader(),但流程不喜欢它:

  

模块./La​​yout [1]不是多态类型。

我该如何解决?

这是我要扩展的类(不是抽象的):
Layout.js

//@flow
import * as React from 'react';
import Header from '../header/Header';
import '../../css/settings/utilities.css';

export type LayoutProps = {
    children: React.Node,
};

class Layout extends React.PureComponent<LayoutProps> {
    renderHeader() {
        return <Header />;
    }
    render() {
        return (
            <React.Fragment>
                <h1 className="u-hd">My Wee Loyalty Prime</h1>
                {this.renderHeader()}
                <main className="main">{this.props.children}</main>
            </React.Fragment>
        );
    }
}

export default Layout;

这是我的课程,应该扩展: LayoutRegistration.js

//@flow
import * as React from 'react';
import Header from '../header/Header';
import * as Layout from './Layout';
import '../../css/settings/utilities.css';

export default class LayoutRegistration extends Layout {
    renderHeader() {
        return <Header showSubnav={false} />;
    }
}

2 个答案:

答案 0 :(得分:2)

尝试一下:

//@flow
import * as React from 'react';
import Header from '../header/Header';
import '../../css/settings/utilities.css';
//Change te type to interface. Since type is not assignable to interface
type LayoutProps<T> {
    children: React.Node
};

class Layout<T> extends React.PureComponent<*, LayoutProps<T>, *> {
    renderHeader() {
        return <Header />;
    }
    render() {
        return (
            <React.Fragment>
                <h1 className="u-hd">My Wee Loyalty Prime</h1>
                {this.renderHeader()}
                <main className="main">{this.props.children}</main>
            </React.Fragment>
        );
    }
}

答案 1 :(得分:2)

问题是

import * as Layout from './Layout';

Layout的导入是模块对象,而应该是一个类。

由于Layout组件是默认导出,因此应为:

import Layout from './Layout';