2
我的頁面/組件輸出一個表單,在提交時調用我的後端API。如果成功,它會返回一個我添加到我的redux商店的對象。如何在componentWillReceiveProps中觸發頁面重定向?
我使用componentWillReceiveProps
爲了在我的組件中知道reducer是否成功地將對象添加到我的商店的屬性。我包含了來自redux-router的Redirect。
import { Redirect } from 'react-router-dom';
componentWillReceiveProps(nextProps) {
if(nextProps.user.activeSubscriptions) {
this.props.user.activeSubscriptions = nextProps.user.activeSubscriptions;
}
if(Object.keys(nextProps.user.properties).length != Object.keys(this.props.user.properties).length) {
console.log("lets redirect.."); // this works
return (<Redirect to={"/account/properties"} />); // this doesn't...
}
}
如何在我的componentWillReceiveProps
中的if()語句中觸發重定向?
它的工作,但它重新加載整個頁面去那個URL(這是好的,如果它是唯一的解決辦法......但果真如此嗎?) – Lazhar
我來補充一種新的方法。 – Martin
或者你可以去'this.rekt ='帳戶/屬性;'和在'渲染(){返回