我是React的新手,想知道如何将点击按钮路由到新页面。这就是我的应用现在的样子。我将对象发布到服务器,然后希望转到新页面。现在,我正在localhost:3000上运行此应用程序,并且想路由到localhost:3000 / orders。
class App extends Component {
placeOrder = () => {
let {cupcakes, startDate, orders} = this.state;
const order = {
cupcakes: cupcakes,
delivery_date: startDate.toISOString()
}
const newOrder = [...orders, order]
this.setState({orders: newOrder})
fetch('http://localhost:4000/cupcakes/orders', {
method: 'POST',
body: JSON.stringify(orders)
})
.then(function(response) {
if(response.ok) {
return response.json();
}
}).then(function(responseBody){
console.log(responseBody)
})
.catch(function(error) {
console.log("Request failed", error);
});
}
render() {
<button onClick={this.placeOrder}>Place Order</button>
}
}
答案 0 :(得分:0)
如果要从前端导航,则需要使用React Router或用于导航的任何设备来实现。我不知道您如何使用前端导航设置来解决这个问题。
另一种选择是在后端中实现它,这取决于您如何构造前端。像res.redirect('/orders')
答案 1 :(得分:0)
如果您想要最简单(但不是最好,React Router确实有用)重定向,则只需执行window.location.href = http://localhost:3000/orders
。