为什么 then 不是一个函数

Why is then is not a function

this.props.promise.then 不是函数错误。为什么?

constructor(props) {
    super(props);
    this.state = {
        loading: true,
        error: null,
        data: []
    };
}

componentDidMount() {
    this.props.promise.then(
        value => this.setState({loading: false, data: value}),
        error => this.setState({loading: false, error: error}));
}

组件调用:

<Child promise={this.state.data} />

this.state.data是一个数组

提前致谢!

this.props.promise.then is not a function error. Why?

因为无论您传递给名为 promise 的组件是什么,实际上都没有承诺。

并且由于您提到 typeof this.props.promise returns 和 object,您可以将代码可视化为本质上是这样做的:

someObjectNamedPromise.then( ... )

并且由于 someObjectNamedPromise 没有一个名为 then 的 属性 指向一个函数,所以您会收到一个错误,准确地告诉您这一点。