码迷,mamicode.com
首页 > 其他好文 > 详细

react 中文文档案例四 (登陆登出按钮)

时间:2019-01-31 15:47:21      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:set   tin   bind   render   element   return   state   click   str   

import React from react;
import ReactDOM from react-dom;

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)
);
  

 

react 中文文档案例四 (登陆登出按钮)

标签:set   tin   bind   render   element   return   state   click   str   

原文地址:https://www.cnblogs.com/Lolita-web/p/10342183.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!