【发布时间】:2017-04-13 23:24:02
【问题描述】:
当我在它之前调用this.setState() 时,我在一个我知道是一个字符串的道具上收到一个流错误。如果我将 setState() 调用移到使用该道具的行之后,错误就会消失。我得到的错误是:
空
此类型与预期的字符串参数类型不兼容
未定义
此类型与预期的字符串参数类型不兼容
这两个错误都发生在同一行代码中。
这是组件的精简版。见componentWillReceiveProps的实现:
import React, { Component } from "react";
import apiRequest from "../../services/apiRequest";
type PropsT = {
endpoint: ?string,
};
export default class ApiLoader extends Component {
props: PropsT
static defaultProps = { endpoint: null }
state = { isFetching: true }
componentWillReceiveProps(nextProps: PropsT) {
if (!nextProps.endpoint) return;
this.setState({ isFetching: true });
this.fetch(nextProps.endpoint); // FLOW ERROR HERE
}
fetch(endpoint: string) {
apiRequest.get(endpoint)
.then(() => this.setState({ isFetching: false }));
}
render(): React.Element<*> {
return <div>Whatever.</div>;
}
}
只需切换componentWillReceiveProps 中最后两行的顺序即可修复它:
this.fetch(nextProps.endpoint); // NO ERROR!
this.setState({ isFetching: true });
这只是 Flow 中的一个错误,还是我遗漏了什么?
【问题讨论】:
标签: javascript reactjs flowtype setstate