反应JSX组件。不能通过流量

时间:2019-01-04 21:31:10

标签: javascript reactjs

以下代码可以按预期运行:

import Button from "./components/Button"
import Excel from "./components/Excel";
import Logo from "./components/Logo";
import ReactDOM from "react-dom";
import React, {Component} from "react";

type Props = {};
type State = {};

class Parent extends Component<Props, State> {

    constructor(props: Props) {
        super(props)
    }

    triggerJson() {
        this.excel.exportJSON();
    }
    triggerCsv() {
        this.excel.exportCSV();
    }
    render() {
        return (
            <div>
                <div style={{display: "inline-block", background: "purple"}}>
                    <Logo/>
                </div>
                <h1> Welcome to The App!^^!</h1>
                <div>
                    <Button onClick={this.triggerJson.bind(this)}>Export JSON</Button>
                    <Button onClick={this.triggerCsv.bind(this)}>Export CSV</Button>
                </div>
                <Excel ref={excel => this.excel = excel} headers={headers} initialData={data} search={true}/>
            </div>
        );
    }
}

但是尝试通过flow(已安装npm install flow-bin)检查会给我以下错误:

Cannot get this.excel because property excel is missing in Parent [1].

Cannot assign excel to this.excel because property excel is missing in Parent [1].

我需要通过单击按钮来调用Excel'sexportJSON函数。我应该如何修改此代码以通过exportCSV控制权?

我遇到的另一个错误是:

flow

1 个答案:

答案 0 :(得分:2)

我认为您只需要以下内容:

class Parent extends Component<Props, State> {
   excel: whateverTheTypeOfThisShouldBe;
   ... (the rest of your class)

这将excel声明为您的Parent类的属性。