Javascript 反应刷新页面
我正在使用react作为前端来更新数据库中的一些信息。问题是,在我更新数据库中的项目后,我希望它是实时的,但每次我进行新的更新时,我必须再次刷新页面。 我的代码看起来像这样Javascript 反应刷新页面,javascript,reactjs,redux,navigation,browser-history,Javascript,Reactjs,Redux,Navigation,Browser History,我正在使用react作为前端来更新数据库中的一些信息。问题是,在我更新数据库中的项目后,我希望它是实时的,但每次我进行新的更新时,我必须再次刷新页面。 我的代码看起来像这样 edit = async (id, quantity, action) => { if (action === "inc") { await this.props.updateItem(id, quantity + 1); } if (action === &qu
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");
};
这是我用来更新项目的函数。
请帮助,提前感谢。查看此文档 请为React组件创建一个构造函数,如下所示
constructor(props) {
super(props);
this.state = {item: {id: 1, 'quantity': 0}; // 0 is default value.
}
每次编辑数量后,请将状态更新为新值
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>)
}
render(){
返回(
项目ID:{this.state.ID}
项目计数:{this.state.quantity}
)
}
有什么问题?使用状态更新或redux存储更新完成调用后更新项目。我不想在代码中添加最后一行到history.push()。此外,我的数据仅在最初请求时来自redux。