gpt4 book ai didi

javascript - 由于函数执行顺序而导致重定向不起作用

转载 作者:行者123 更新时间:2023-12-02 21:32:45 24 4
gpt4 key购买 nike

我有一个登录页面。如果登录成功并且 token 存在于本地存储中,我想重定向到私有(private)页面/面板。我正在调用表单的 onSubmit() 上的所有函数。

这是我的代码:

function LoginPage (){
const [state, setState] = useState({
email: '',
password: '',
});

const [submitted, setSubmitted] = useState(false);

function ShowError(){
if (!localStorage.getItem('token'))
{
console.log('Login Not Successful');
}
}

function FormSubmitted(){
setSubmitted(true);
console.log('Form submitted');
}

function RedirectionToPanel(){
console.log('ha');
if(submitted && localStorage.getItem('token')){
console.log('FInall');
return <Redirect to='/panel'/>
}
}

function submitForm(LoginMutation: any) {
const { email, password } = state;
if(email && password){
LoginMutation({
variables: {
email: email,
password: password,
},
}).then(({ data }: any) => {
localStorage.setItem('token', data.loginEmail.accessToken);
})
.catch(console.log)
}
}

return (
<Mutation mutation={LoginMutation}>
{submitted && <Redirect to='/panel'/>}
{(LoginMutation: any) => (
<Container component="main" maxWidth="xs">
<CssBaseline />
<div style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center'
}}>
<Avatar>
<LockOutlinedIcon />
</Avatar>
<Typography component="h1" variant="h5">
Sign in
</Typography>
<Formik
initialValues={{ email: '', password: '' }}
onSubmit={(values, actions) => {
setTimeout(() => {
alert(JSON.stringify(values, null, 2));
actions.setSubmitting(false);
}, 1000);
}}
validationSchema={schema}
>
{props => {
const {
values: { email, password },
errors,
touched,
handleChange,
isValid,
setFieldTouched
} = props;
const change = (name: string, e: any) => {
e.persist();
handleChange(e);
setFieldTouched(name, true, false);
setState( prevState => ({ ...prevState, [name]: e.target.value }));
};
return (
<form style={{ width: '100%' }}
onSubmit={e => {e.preventDefault();
submitForm(LoginMutation);FormSubmitted();RedirectionToPanel()}}>
<TextField
variant="outlined"
margin="normal"
id="email"
fullWidth
name="email"
helperText={touched.email ? errors.email : ""}
error={touched.email && Boolean(errors.email)}
label="Email"
value={email}
onChange={change.bind(null, "email")}
/>
<TextField
variant="outlined"
margin="normal"
fullWidth
id="password"
name="password"
helperText={touched.password ? errors.password : ""}
error={touched.password && Boolean(errors.password)}
label="Password"
type="password"
value={password}
onChange={change.bind(null, "password")}
/>
{submitted && ShowError()}

<FormControlLabel
control={<Checkbox value="remember" color="primary" />}
label="Remember me"
/>
<br />
<Button className='button-center'
type="submit"
disabled={!isValid || !email || !password}
// onClick={handleOpen}
style={{
background: '#6c74cc',
borderRadius: 3,
border: 0,
color: 'white',
height: 48,
padding: '0 30px'
}}
>
Submit</Button>
</form>
)
}}
</Formik>
</div>
</Container>
)
}
</Mutation>
);
}

export default LoginPage;

当我点击提交按钮时,我会检查控制台以了解 RedirectionToPanel() 函数内部发生的情况。第一次,打印“Ha”,但当我第二次单击它时,同时打印“Ha”和“Finally”。但是,重定向仍然没有发生。

如果我使用{submitted && <Redirect to='/panel'/>}突变后,我在突变时收到此错误:

This JSX tag's 'children' prop expects a single child of type '(mutateFunction: MutationFunction<any, Record<string, any>>, result: MutationResult<any>) => Element | null', but multiple children were provided.ts(2746)

如果我在返回之后和突变之前使用它,我会在 && 和 } 上收到语法错误。

最佳答案

正如@wxker所说,你需要返回<Redirect> render 方法中的元素,以便实际发生重定向。例如。 https://codesandbox.io/s/proud-rgb-d0g8e .

关于javascript - 由于函数执行顺序而导致重定向不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60579292/

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