zoukankan      html  css  js  c++  java
  • React-条件渲染

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8" />
        <title>Learn React</title>
        <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
        <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
        <!-- 生产环境中不建议使用 -->
        <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
    </head>
    <body>
    <div id="root"></div>
    <script type="text/babel">
    
    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/>;
        }else{
            return <GuestGreeting/>
        }
    }
    
    function LoginButton(props){
        return(
            <button onClick={props.onClick}>
                Login
            </button>
        );
    }
    function LogoutButton(props){
        return(
            <button onClick={props.onClick}>
                Logout
            </button>
        );
    }
    
    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>
        );
        }
    }
    
    ReactDOM.render(
    <LoginControl />,
        document.getElementById('root')
    );
    
    </script>
    </body>
    </html>
    
    
  • 相关阅读:
    控制器生命周期逻辑调用
    数据持久化
    Mac屏幕录制Gif
    iOS开发应用上架必读最新苹果审核规则
    过滤字符串中的非汉字、字母、数字
    文字加描边
    博客全局修改需求
    iOS Xcode12 运行iOS15系统程序卡在启动页要等很久才能进入主页
    macOS环境:安装Go(21-10-22完)
    关闭WIN10自动配置 IPV4 地址 169.254解决方法
  • 原文地址:https://www.cnblogs.com/csnd/p/12061876.html
Copyright © 2011-2022 走看看