flow - 无法使用'Home'扩展'Component',因为object literal [2]与属性'state中的undefined [3]不兼容

问题描述 投票:0回答:1

我正在尝试使用Flow与反应,我按照一些教程,但我无法解决此错误,我的代码看起来像这样:

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>

有效!但是你想明白为什么吗?

reactjs flowtype flow-typed
1个回答
1
投票

React flow definitions中,组件的泛型类型参数首先是Props,然后是state。正如您所注意到的那样,首先提供Props类型然后提供状态就是您想要做的事情。

© www.soinside.com 2019 - 2024. All rights reserved.