gpt4 book ai didi

javascript - 如果这是用户访问的第一个组件,则将用户重定向到另一个组件

转载 作者:行者123 更新时间:2023-11-28 03:50:17 26 4
gpt4 key购买 nike

我有一张注册表。用户填写所有需要的数据,然后单击提交按钮,他将被重定向到确认页面。

这是一个简单的页面/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 组件上,设置一个指定的 prop 并检查它是否存在于路由中?

期待任何提示。谢谢你!

最佳答案

您可以在 componentDidMount 中测试您的条件,如果它是虚假重定向:

componentDidMount = () => {
if (!yourCondition) {
return this.props.history.push({
pathname: '/register-account'
});
}

}

也许还有其他解决方案:

尝试像这样重定向:

import { Redirect } from 'react-router-dom';

并在渲染方法中使用:

if (!yourCondition) { return <Redirect to="/register-account" /> }

关于javascript - 如果这是用户访问的第一个组件,则将用户重定向到另一个组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48051594/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com