您现在的位置:首页 >> 前端 >> 内容

React按钮登入代码实现

时间:2017/11/18 11:48:24 点击:

  核心提示:React按钮登入代码实现htmlheadtitleDocument/titlescript src=../react.js/scriptscript src=../react-dom.js/scri...

React按钮登入代码实现

<html>
  <head>
      <title>Document</title>
      <script src="../react.js"></script>
      <script src="../react-dom.js"></script>
      <script src="https://cdn.bootcss.com/babel-core/5.8.38/browser.min.js"></script>
  </head>

  <body>
      <p id="reactContainer"/>

      <script type="text/babel">
        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 = null
            if (!isLoggedIn) {
              button = <LoginButton onClick={this.handleLoginClick} />
            } else {
              button = <LogoutButton onClick={this.handleLogoutClick} />
            }

            return (
              <p>
                <Guest isLoggedIn={isLoggedIn} /> //can not nesting
                {button}
              </p>
            )
          }
        }

        function LoginButton(props) {
          return (
            <button onClick={props.onClick}>
              Login
            </button>
          )
        }

        function LogoutButton(props) {
          return (
            <button onClick={props.onClick}>
              Logout
            </button>
          )
        }

        function UserGreeting(props) {
          return <h1>welcome back</h1>
        }

        function GuestGreeting(props) {
          return <h1>please sign up</h1>
        }

        function Guest(props) {
          const isLoggedIn = props.isLoggedIn

          if (isLoggedIn) {
            return <UserGreeting />
          }

          return <GuestGreeting />
        }

        ReactDOM.render(
          <LoginControl/>,
          document.getElementById('reactContainer')
        )
    </script>
  </body>
</html>

 

Tags:RE EA AC CT 
作者:网络 来源:qq_3435173