简体   繁体   English

在 React 和 setState 中有条件地渲染组件/元素时出现问题

[英]Trouble conditionally rendering components/elements in React and setState

I have a react login/signup form that when you land on the page you are on the signup section, I need to display the login section when clicking log in, and to display the products like it does when you click Get Started from either of the signup or login components, with the signup section disappearing.我有一个反应登录/注册表单,当您登陆页面时,您在注册部分,我需要在单击登录时显示登录部分,并显示产品,就像您从其中一个单击开始时一样注册或登录组件,注册部分消失。 Both signup and login should virtually be the same just different inputs and display in the same space.注册和登录实际上应该是相同的,只是不同的输入并显示在同一个空间中。

My issue is I have the signup and Get started portion displaying and functioning correctly, but cant seem to figure out how to correctly set the state for when a user clicks the login button and having it switch to display that component instead of signup.我的问题是我的注册和入门部分显示和运行正常,但似乎无法弄清楚如何正确设置 state,以便用户单击登录按钮并让它切换以显示该组件而不是注册。

I believe my part of my issue is in this statement:我相信我的部分问题在于以下声明:

render() {
    const { requestedPostsThatWeGotFromGecko } = this.state;
    const { loginStatus } = this.state;
    return (
       <div className="gecko">
        {requestedPostsThatWeGotFromGecko ? (
          <Cards />
        ) : loginStatus ? (
            <Login signup={() => this.clickMe()} />
        ) : (
          <SignUp login={() => this.clickMe()} />
        )
        }
      </div> 
    );
  }

The Gecko is my main app structure pulling most of it in, with signup, login, and cards being my other components. Gecko 是我的主要应用程序结构,其中大部分都包含在其中,注册、登录和卡片是我的其他组件。

SignUp Component注册组件

 import React from 'react'; import Login from '../LogIn'; //import CoolTabs from 'react-cool-tabs'; export default class SignUp extends React.Component { render() { const onClick = () => { this.props.login(); console.log('rich'); } return ( <div className='sign-up'> <table className='sign-up-form'> <tbody> <div class="gecko-signup__tabs"><button id="gecko-signup" data-selected="yes">Sign Up</button> <button id="gecko-login" data-selected=""onClick={this.setState.loginClose}>Log In</button></div>... <td colSpan="2"><input id="getStarted" type="submit" value="Get Started" onClick={onClick}/></td> </tr> </tbody> </table> </div> ); } }

Login Component登录组件

 import React from 'react'; import SignUp from '../SignUp'; export default class Login extends React.Component { render() { const onClick = () => { this.props.signup(); console.log('rich'); } return ( <div className='login'> <div className="login" ref={el => (this.div = el)}></div> <table className='sign-up-form'> <tbody> <div class="gecko-signup__tabs"> <input id="gecko-signup" value="Sign Up" onClick={SignUp} /> <input id="gecko-login" value="Log In" onClick="" /> </div> <tr> <td> <p id="signUpFree">Welcome Back</p> </td> </tr> <div id="inputs-section"> <div class="field-wrap"> <label>Email Address<span class="req">*</span></label> <input type="email"required autocomplete="off"/> </div> <div class="field-wrap"> <label>Password<span class="req">*</span></label> <input type="password"required autocomplete="off"/> </div> <tr> <td colSpan="2"><input id="getStarted" type="submit" value="Log Me In" onClick={onClick}/></td> </tr> </div> </tbody> </table> </div> ); } }

Is there a simple way to alternate between these three based on default value, or what button is being pressed?有没有一种简单的方法可以根据默认值在这三个之间交替,或者按下什么按钮? I have looked at react-router-dom but found myself tearing a lot of my functionality so far apart.我查看了 react-router-dom,但发现自己已经将很多功能分开了。

  1. You could add the auth type as the state in your React component.您可以在 React 组件中将身份验证类型添加为state
  2. Depending on what the user has clicked, update the React state.根据用户点击的内容,更新 React state。
  3. Know the component corresponds to the state.知道该组件对应于 state。
this.state = {
 authType: "signup"
}

render() {
  if (authType === "signup") return <Signup />
  if (authType === "login") return <Login />
}

or better,或更好,

you could use different routes for login and signup using react-router .您可以使用react-router使用不同的登录和注册路线。 And show them differently.并以不同的方式展示它们。

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

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