我正在创建一个简单的index.html
应用程序,我希望在用户创建产品后将其重定向到产品预览页面。
我尝试在eCommerce
中使用<Redirect />
来实现这一点
下面的代码段:
UploadProduct component
然后在render方法中,仅当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
});
}
代码传递到uploadProduct component
:
props
我希望在将新产品创建到<Route path='/product/new'
render={(routeProps)=> <UploadProduct {...routeProps} productCreated= {this.handleProductCreated} /> } />
时将重定向,但我收到此错误消息:
product preview page
我将被重定向到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.
答案 0 :(得分:0)
您需要的是异步等待。确保在其中进行API调用的函数正在使用异步等待。由于您尚未发布API调用函数。我只准备了很少的部分,并在ES6中编写了以下代码。
createProduct = async dataForApi => {
await makeApiCallForCreatingProduct(dataForApi);
this.setState({ redirect: true })
};