[英]Redirect user to another component if it was the first component he visited
我有一張注冊表。 用戶填寫所有需要的數據,然后單擊“ submit
按鈕,並將其重定向到確認頁面。
這是一個簡單的頁面/confirm-account
,顯示了他輸入的所有數據,因此他可以確保自己正確輸入了所有數據。
但是 ,我不希望用戶直接從瀏覽器輸入此/confirm-account
,而是僅在單擊submit
按鈕后才能輸入。 如果他只是打開瀏覽器並粘貼了此鏈接並按Enter,它將把他重定向到/register-account
。
我的嘗試:( routes.js
)
path: 'confirm-account',
name: 'confirmAccount',
indexRoute: { onEnter: (nextState, replace) => {
if (state doesn\'t have something, I dont know yet) { <-------------- condition
replace('/register-account')
} },
getComponent(nextState, cb) {
const importModules = Promise.all([
import('containers/ConfirmAccount'),
]);
問 :條件內用什么?
重要說明 :提交時,我使用的是:
this.props.dispatch(push('/confirm-account'));
我的第一個想法是使用該push
函數傳遞一些數據,並檢查路由文件中是否可用。 或者,如果用戶在register-account
組件中,則設置一個指定的道具,然后僅檢查其是否存在於路由中?
期待任何提示。 感謝你!
您可以在componentDidMount中測試您的條件,如果重定向錯誤:
componentDidMount = () => {
if (!yourCondition) {
return this.props.history.push({
pathname: '/register-account'
});
}
}
其他解決方案可能是:
嘗試這樣重定向:
import { Redirect } from 'react-router-dom';
並在render方法中使用:
if (!yourCondition) { return <Redirect to="/register-account" /> }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.