我有一个WithLoader组件,该组件会显示加载程序,直到所有内容正确加载为止,然后呈现props.children,这意味着handleOnLoading已执行并设置了响应,但似乎react试图呈现props.children。即使未装入WithLoader。为什么这种行为呢?仅在WithLoader成功安装并且handleOnLoading等待服务器响应并将值设置为组件状态之后,才可以显示props.children吗?
const handleOnLoading = async () => {
const response = await getResponse();
setResponse(response);
};
<WithLoader onLoading={handleOnLoading}>
<SomeComponent someProperty={response.field}/> //props.children for WithLoader
</WithLoader>
class WithLoader {
constructor(props) {
super(props);
this.state = {
isLoading: true,
};
}
...
componentDidMount() {
this.props
.onLoading()
.then(() => {
this.setState({ isLoading: false });
})
.catch((err) => {
this.setState({ isLoading: false, error: err.message });
});
}
render() {
return this.state.isLoading ? (
<Loader />
) : this.state.error ? (
<UserFeedback feedback={{ error: this.state.error }} />
) : (
<>{this.props.children}</>
);
}
}