我正在使用该功能来更新数据库中的某些信息,并以react作为前端。问题是,在我更新数据库中的项目后,我希望它是实时的,但是每次我进行新的更新时,我都必须再次刷新页面。 我的代码看起来像这样。
edit = async (id, quantity, action) => {
if (action === "inc") {
await this.props.updateItem(id, quantity + 1);
}
if (action === "dec") {
await this.props.updateItem(id, quantity - 1);
}
history.push("/");
history.push("/cart");
};
这是我用来更新商品的功能。 请帮忙,谢谢。
答案 0 :(得分:0)
查看此文档
https://reactjs.org/docs/state-and-lifecycle.html#adding-local-state-to-a-class
请为React组件创建一个构造函数,如下所示
constructor(props) {
super(props);
this.state = {item: {id: 1, 'quantity': 0}; // 0 is default value.
}
每购买edit
数量后,请将状态更新为新值
this.setState({'id': 1, quantity: this.state.quantity + 1});
现在,使用状态呈现HTML
render() {
return (<div>
<h1>Item ID: {this.state.id}</h1>
<h2>Item Count: {this.state.quantity}</h2>
</div>)
}