流-无法用'Home'扩展'Component',因为对象文字[2]与属性'state中的未定义[3]不兼容

时间:2018-11-25 23:30:50

标签: reactjs flowtype flow-typed

我正在尝试将Flow与react配合使用,我遵循了一些教程,但是我无法解决此错误,我的代码如下所示:

import React, { Component } from "react";
import { Link, withRouter } from "react-router-dom";

import { login } from "../../services/auth";
import api from "../../services/api";
import { getToken } from "../../services/auth";

import type { RouterHistory } from "react-router-dom";

type State = {
    username: number,
    password: string,
    history: RouterHistory,
}

class Home extends Component < State > {
    handleChange = this.handleChange.bind(this);
    handleSignIn = this.handleSignIn.bind(this);

    state = {
        username: '',
        password: '',
        history: '',
    };

错误出现在组件内部的状态。

编辑1: 添加:

...
type Props = {/ * ... * /};
...
class Home extends Component <Props, State>

成功了!但是你想知道为什么吗?

1 个答案:

答案 0 :(得分:1)

React flow definitions中,组件的通用类型参数首先是Props,然后是状态。因此,您已经注意到,首先要提供Props类型,然后说明您要执行的操作。