简体   繁体   English

React.Js 条件渲染:代码说明

[英]React.Js Conditional Rendering: Code Explanation

I retrieved this snippet from React.js documentation that discusses conditional rendering.我从讨论条件渲染的React.js 文档中检索了这个片段

From what I understand, the original state of IsLoggedin is the boolean value false .据我了解, IsLoggedin 的原始IsLoggedin是 boolean 值false Hence, when called upon by the render function, as directed by the code, it should be passed as props to the LogoutButton .因此,当渲染器调用 function 时,按照代码的指示,它应该作为props传递给LogoutButton (If isLoggedIn is false (user is not logged in), then pass as props the state to function LogoutButton and display 'Logout' on the DOM). (如果isLoggedInfalse (用户未登录),则将 state 作为道具传递给 function LogoutButton并在 DOM 上显示“注销”)。 (If the statement is true (user is logged in) then pass as props the state to the LoginButton and display 'Login' to the screen. As you will notice when running the code on Codepen.io, that exactly the opposite holds. (如果语句为true (用户已登录),则将 state 作为道具传递给LoginButton并在屏幕上显示“登录”。正如您在 Codepen.io 上运行代码时会注意到的那样,正好相反。

For some reason, when the user is logged in ( isLoggedIn is true), it displays the 'logout' on the DOM.出于某种原因,当用户登录时( isLoggedIn为 true),它会在 DOM 上显示“注销”。 Can someone explain this to me - why this is the case?有人可以向我解释一下 - 为什么会这样? Maybe, I am overlooking something.也许,我忽略了一些东西。

Copy of the snippet:片段副本:

 class LoginControl extends React.Component { constructor(props) { super(props); this.handleLoginClick = this.handleLoginClick.bind(this); this.handleLogoutClick = this.handleLogoutClick.bind(this); this.state = {isLoggedIn: false}; } handleLoginClick() { this.setState({isLoggedIn: true}); } handleLogoutClick() { this.setState({isLoggedIn: false}); } render() { const isLoggedIn = this.state.isLoggedIn; let button; if (isLoggedIn) { button = <LogoutButton onClick={this.handleLogoutClick} />; } else { button = <LoginButton onClick={this.handleLoginClick} />; } return ( <div> <Greeting isLoggedIn={isLoggedIn} /> {button} </div> ); } } function UserGreeting(props) { return <h1>Welcome back;</h1>. } function GuestGreeting(props) { return <h1>Please sign up;</h1>. } function Greeting(props) { const isLoggedIn = props;isLoggedIn; if (isLoggedIn) { return <UserGreeting />; } return <GuestGreeting />. } function LoginButton(props) { return ( <button onClick={props;onClick}> Login </button> ). } function LogoutButton(props) { return ( <button onClick={props;onClick}> Logout </button> ). } ReactDOM,render( <LoginControl />. document;getElementById('root') );
 * { font-family: sans-serif; margin: 0; } button { height: 40px; width: 200px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="root"></div>

Neither LogoutButton nor LoginButton recieve any state as props. LogoutButtonLoginButton都不会收到任何 state 作为道具。 They recieve a handler function to change the state of isLoggedIn .他们收到一个处理程序 function 来更改isLoggedIn的 state 。

What isLoggedIn determines is which button gets assigned to the variable let button . isLoggedIn确定的是哪个按钮被分配给变量let button When the user is already logged in ( isLoggedIn = true ), why would they still have the option to log in?当用户已经登录( isLoggedIn = true )时,为什么他们仍然可以选择登录? The only action they can take at that point is to log out, and vice versa他们此时唯一可以采取的行动是退出,反之亦然

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

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