【发布时间】:2021-02-07 21:06:59
【问题描述】:
这是组件代码。 它被包裹在连接函数中,并适当设置了 mapStateToProps 和 mapDispatchToProps。
//Parent component
componentDidMount()
{this.props.dispatchAction()}//dispatches action which sets requestPending state in redux to true and fetches data. when the data is fetched it sets requestPending to false again.
然后在父组件的render方法中。
if (this.props.requestPending) return <Loading />;
if (!this.props.requestPending) return <SecondChild {...this.props.data} />;
问题在于,当父组件挂载时,分秒 requestPending 为 false,因此它呈现 SecondChild 组件。这给出了未定义的错误,因为 this.props.data 尚未获取。
渲染这个 JSX 的适当方式是什么?
【问题讨论】:
-
你应该从你的代码中提供更多。
标签: javascript reactjs