简体   繁体   English

ReactJS 钩子在单击登录按钮时未指向其他组件

[英]ReactJS hooks not directing to other component while clicking login button

I am trying to render a new component in reactjs when my username and password matches the value, but it is not rendering to other component but giving the result in console.log当我的用户名和密码与该值匹配时,我试图在 reactjs 中呈现一个新组件,但它不会呈现给其他组件,而是在 console.log 中给出结果

const loginSubmit = (e) => {
            e.preventDefault()
            if(username === 'molly' && password === '123'){
                console.log('ok')
            }
        }

When i write above code for calling function from button, console.log prints 'ok' but when I write the code as当我编写上述代码以从按钮调用 function 时,console.log 打印“确定”但是当我将代码编写为

const loginSubmit = (e) => {
            e.preventDefault()
            if(username === 'molly' && password === '123'){
                return(<Post />)
            }
        }

why is it not rendering Post component???为什么不渲染 Post 组件??? It gets stuck in the login page only after clicking submit button.只有在单击提交按钮后,它才会卡在登录页面中。 Help me out please请帮帮我

If it is redirecting to new page you could do it like this.如果它重定向到新页面,您可以这样做。

import React from 'react';
import { useHistory } from 'react-router-dom';

const MyComponent = () => {
    const history = useHistory();

    const loginSubmit = (e) => {
            e.preventDefault();
            if(username === 'molly' && password === '123'){
                history.push("/path/to/redirect");
            }
        }

   

    return (
        <div>
            <button onClick={loginSubmit} type="button" />
        </div>
    );
}

export default MyComponent;

You're only returning the component you'd like to render to the caller of the function.您只是将要呈现的组件返回给 function 的调用者。 When the function is called, the Post component is returned to whatever called the function, but what you probably want is to 'return' it from a function component.当调用 function 时,Post 组件将返回到称为 function 的任何位置,但您可能想要从 ZC1C425268E68385D1AB5074C17A94F 组件“返回”它。

Try rendering it from your parent / page component conditionally based on whether a variable like 'isAuthenticated' is true:尝试根据像“isAuthenticated”这样的变量是否为真有条件地从您的父/页面组件呈现它:

{isAuthenticated && <Post />}

then use your custom function to set the isAuthenticated variable to true when the credentials match.然后在凭据匹配时使用您的自定义 function 将 isAuthenticated 变量设置为 true。

EDIT: updating based on your comments below the question.编辑:根据您在问题下方的评论进行更新。 If that component is already set to render on the /posts route, just redirect to the /posts route when isAuthenticated is true on formSubmit, then let the component do the rendering automatically.如果该组件已经设置为在 /posts 路由上渲染,只需在 formSubmit 上的 isAuthenticated 为 true 时重定向到 /posts 路由,然后让组件自动进行渲染。

You can try something like this:你可以尝试这样的事情:

 //Hook Logged const [LoggedIn, setLoggedIn] = useState(false) const loginSubmit = (e) => { e.preventDefault() if(username === 'molly' && password === '123'){ setLoggedIn(true) } } if (LoggedIn() ) return ( <div> <Post /> </div> ) else return ( <div> <LoginForm/> </div> )

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

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