class SubCategories extends React.Component<
SubCategoryStateProps
> {
constructor(props: RouteComponentProps<CategoryUrlParams>) {
super(props);
this.state = {
category: null,
};
}
componentDidMount() {
axios
.get(
window.location.origin + `/core/academix/categories/${this.CategoryId}`
)
.then((result: AxiosResponse<Category>) => {
if (result.status == 200) {
this.setState({
category: result.data,
});
}
})
.catch((error) => {
handleApiError(
error,
'Something went wrong when trying to load category details'
);
});
};
render() { <Title level={3}>{this.state.category.translations[1].name}</Title>);
}
}
export default SubCategories;
在初始状态下,我将category
设置为null,然后在SubCategoryStateProps中,将其Categpry | null
设置为category
的新状态,但是在渲染之后,它仍然显示类别是axios
为什么?
您需要使null
异步