2 回答
TA贡献1802条经验 获得超10个赞
手册和视频数据处于您的 HelpList 组件的状态,但您正在使用 this.props.data.map,它使用 data={this.state.videos} 从您的 HelpListAdmin 中变为空。
所以你需要在你的 HelpList 组件的渲染中替换这样的相关代码。
{this.state.manuals.map((card: Manual) => (
<HelpCard
card={card}
key={card.id}
deleteProduct={this.props.onDelete}
editProduct={this.props.onEdit}
type={this.props.type}
/>
))}
但是如果你想在你的 HelpAdminView 组件中管理状态,你需要在这个组件而不是 HelpList 组件中进行 api 调用。
而且我认为您应该将状态保留在父级 (HelpAdminView) 中,并将手册和视频传递给 HelpList 组件。
因此,您需要将此代码从 HelpList 移至 HelpAdminView。
async componentDidMount() {
const res = await axios.get(this.state.url);
this.setState({ card: res.data });
this.loadAdminHelpCard("videos");
this.loadAdminHelpCard("manuals");
//console.log(res.data);
}
loadAdminHelpCard = (type: "videos" | "manuals"): void => {
const baseApiUrl = `http://localhost:3000`;
const url = `${baseApiUrl}/${type}`;
axios
.get(url)
.then((res) => {
this.setState({ [type]: res.data } as any);
})
.catch(function(error) {
// handle error
console.log(error);
});
};
移除 HelpList 组件中的状态。
并将视频和手册作为道具(就像您现在所做的那样)传递给 HelpList 组件。
<div className="listDisplay">
<div>
<div style={{ textAlign: "center" }}>
<h2>Videos</h2>
</div>
<HelpList
data={this.state.videos}
type="videos"
onEdit={this.editProduct}
onDelete={this.deleteProduct}
/>
</div>
<div>
<div style={{ textAlign: "center" }}>
<h2>Manuals</h2>
</div>
<HelpList
data={this.state.manuals}
type="manuals"
onEdit={this.editProduct}
onDelete={this.deleteProduct}
/>
</div>
</div>
并在 HelpList 组件中使用这个道具(就像你现在所做的那样)。
export default class HelpList extends Component<Props, State> {
static props: any;
render() {
console.log(this.props.data);
return (
<React.Fragment>
{this.state.card ? (
<div className="row">
{this.props.data.map((card: Manual) => (
<HelpCard
card={card}
key={card.id}
deleteProduct={this.props.onDelete}
editProduct={this.props.onEdit}
type={this.props.type}
/>
))}
</div>
) : (
<h1>
<br></br>Loading Cards...
</h1>
)}
</React.Fragment>
);
}
}
TA贡献1818条经验 获得超11个赞
您正在 HelpList 组件中调用 API 并将其设置为 state 在您的 HelpAdminView 组件中,您有一个手册状态,它是一个空数组并将其作为数据道具传递,因为您没有更新它,它将保持为空,
您可以在 HelpAdminView 组件中调用 API,然后将其作为道具传递给您的 HelpList 组件
希望能帮助到你
添加回答
举报