我正在尝试使用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>
有效!但是你想明白为什么吗?
在React flow definitions中,组件的泛型类型参数首先是Props,然后是state。正如您所注意到的那样,首先提供Props类型然后提供状态就是您想要做的事情。