简体   繁体   English

如何自动点击提交按钮 - 反应

[英]How to automatically click submit button - react

I have a react login page with username and password manually inputted,我有一个手动输入用户名和密码的反应登录页面,

I want the submit button to automatically click upon page load so it will automatically log in users.我希望提交按钮在页面加载时自动单击,以便自动登录用户。

Here is my code.这是我的代码。


export const LoginForm = () => {
    const history = useHistory();
    const classes = useStyles();
    const email = 'email@email.com';
    const password = 'mypassword';
    const [authStatus, setStatus] = useState(null);
    const [isSpinnerHidden, setSpinner] = useState(true);
    const { session } = useContext(SessionContext);

    const recaptchaRef = useRef();

    const _onSubmit = async (e) => {
        e.preventDefault();
        setSpinner(false);

        const authResult = await session.signIn(email, password);
        setSpinner(true);
        console.log(session);
        authResult ? setStatus(authResult) : history.push('/dashboard');

    };

    return (
        <form onSubmit={_onSubmit}>
            <Typography
                align='center'
                color='primary'
                variant="h4"
                style={{ marginBottom: '20px' }}
            >
                Sign In
            </Typography>
            {authStatus ?
                <Alert
                    severity="error"
                >
                    {authStatus}
                </Alert>
                :
                null
            }
            <TextField
                type='email'
                label="Email"
            />
            <TextField
                type='password'
                label="Password"
            />
            <Link
                onClick={() => history.push('/restore')}
                className={classes.restore}>
                Forgot password?
            </Link>

            
            <MuiButton
                text='Sign In'
                type='submit'
                value='login'
                isSpinnerHidden={isSpinnerHidden}
                className={classes.button}
            />
        </form>

        
    );
};

what can I add to this code or change that will make it click on the sign-in button automatically and login the user.我可以在此代码中添加什么或进行更改以使其自动单击登录按钮并登录用户。

I don't think clicking the button automatically is the best experience you can have here.我不认为自动单击按钮是您在这里可以获得的最佳体验。

The better way of doing that here would be to conditionally choose the route based on authentication state before you even render the login screen.更好的方法是在渲染登录屏幕之前根据身份验证 state 有条件地选择路由。 Something like this:像这样的东西:

if (isAuthenticated){
  history.push('/dashboard);
} else {
  history.push('/login');
}

If for whatever reason you NEED to do it your way, just call your signin function when the components mounts with useEffect如果出于某种原因您需要按照自己的方式进行操作,只需在使用 useEffect 安装组件时调用您的登录 function

import React, {useEffect} from 'react';
export const LoginForm = () => {
    
    ...
  useEffect(() => {
    if(isAuthenticated){ // You'll need to get the auth state from somewhere here
     history.push('/dashboard')
     }
  },[])

    return (
     ...
       
    );
};

In order to automatically click on the submit button, you need to use an onLoad为了自动点击提交按钮,你需要使用一个onLoad

convert your _onSubmit to a windows onload, something like this将您的 _onSubmit 转换为 windows onload,像这样

window.onload = async (e) => {
    e.preventDefault();
    setSpinner(false);
    console.log('hello');
    const authResult = await session.signIn(email, password);
    setSpinner(true);
    console.log(session);
    authResult ? setStatus(authResult) : history.push('/dashboard');

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM