【发布时间】:2019-08-04 16:48:03
【问题描述】:
我正在创建一个简单的eCommerce 应用程序,我想在用户创建产品后将他们重定向到产品预览页面。
我已经尝试在我的UploadProduct component 中使用<Redirect /> 来实现这一点
下面的代码sn-p:
state = {
productName: '',
productSize: '',
productPrize: '',
category: '',
description: '',
// color: [],
imageFile: null,
redirect: false
};
在渲染方法中,我仅在this.state.redirect 时有条件地重定向
是的,我在axios post request 之后更新,下面显示了我是如何实现重定向的。
const {redirect} = this.state;
if(redirect){
console.log("Im here redirect now", redirect);
return <Redirect from='/product/new' to='/product/preview' />
}
我的App.js
状态:
state = {
products: [],
productID: null,
noProducts: false
};
我更新它的state 使用:
handleProductCreated = (productID) => {
this.setState({
productID //es6
});
}
作为props下面的代码传递给uploadProduct component:
<Route path='/product/new'
render={(routeProps)=> <UploadProduct {...routeProps} productCreated= {this.handleProductCreated} /> } />
我希望在创建新产品时被重定向到 product preview page put 我收到此错误消息:
index.js:1375 Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
我被重定向到/notfound
【问题讨论】:
标签: reactjs react-router react-router-v4 react-router-dom