zheng 6 giorni fa
parent
commit
cc60e17329
2 ha cambiato i file con 162 aggiunte e 0 eliminazioni
  1. 83 0
      21.react/初阶/15.条件渲染.html
  2. 79 0
      21.react/初阶/16.列表渲染.html

+ 83 - 0
21.react/初阶/15.条件渲染.html

@@ -0,0 +1,83 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+    <script src="./babel.min.js"></script>
+    <script src="./react.development.js"></script>
+    <script src="./react-dom.development.js"></script>
+</head>
+
+<body>
+    <div id="root"></div>
+    <!-- v-if React 没有
+        实现条件渲染:
+            三元判断
+            if else 
+            switch case 
+    -->
+    <script type="text/babel">
+        class App extends React.Component {
+            constructor() {
+                super();
+                this.state = {
+                    isLogin: true
+                }
+                this.handleClick = this.handleClick.bind(this)
+            }
+
+            // LoginMain = () => {
+            //     console.log("你好")
+            //    return this.state.isLogin ? (
+            //         <div>已登录</div>
+            //     ) : (
+            //         <div>未登录</div>
+            //     )
+            // }
+            // LoginMain = ()=> {
+            //     if(this.state.isLogin) {
+            //         return   <div>已登录</div>
+            //     }else {
+            //         return   <div>未登录</div>
+            //     }
+            // }
+            LoginMain = ()=> {
+                switch(this.state.isLogin) {
+                    case true :
+                            return  <div>已登录</div>
+                    case '111':
+                            return <div>111</div>
+                    default:   
+                            return   <div>未登录</div> 
+                    }
+            }
+            handleClick() {
+                this.setState({
+                    isLogin: !this.state.isLogin
+                })
+            }
+            render() {
+                // return (
+                //     <div>
+                //         <h1>条件渲染</h1>
+                //         {this.LoginMain()}
+                //         <button onClick={this.handleClick}>修改状态</button>
+                //     </div>
+                // )
+               return( 
+                this.state.isLogin ? 
+                 (<div>已登录</div>) : 
+                 ( <div>未登录</div>)
+                )
+            }
+        }
+        const element = <App/>
+        const container = document.getElementById("root");
+        const root = ReactDOM.createRoot(container)
+        root.render(element);
+    </script>
+</body>
+
+</html>

+ 79 - 0
21.react/初阶/16.列表渲染.html

@@ -0,0 +1,79 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+    <script src="./babel.min.js"></script>
+    <script src="./react.development.js"></script>
+    <script src="./react-dom.development.js"></script>
+</head>
+
+<body>
+    <div id="root"></div>
+    <!-- 
+        React主要使用的是Js方法(map/forEach/for)
+    -->
+    <script type="text/babel">
+        class App extends React.Component {
+            constructor() {
+                super();
+                this.state = {
+                    list: [
+                        {
+                            id:1,
+                            name:'图图',
+                            age:3
+                        },
+                        {
+                            id:2,
+                            name:'小美',
+                            age:4
+                        },
+                        {
+                            id:3,
+                            name:'大壮',
+                            age:5
+                        }
+                    ]
+                }
+                this.renderMain = this.renderMain.bind(this)
+            }
+            renderMain() {
+                // console.log("走进来")
+                // return  this.state.list.map((item) => {
+                //     return <p>我叫{item.name}</p>
+                // }) 
+                let newList = [];
+                // this.state.list.forEach((item) =>  {
+                //     newList.push(<p>我叫{item.name}</p>)
+                //     console.log(newList,'1')
+                // })
+                // console.log(newList)
+                // return newList;
+                for(let i=0;i<this.state.list.length;i++) {
+                    newList.push(
+                         <p>我叫{this.state.list[i].name}</p>
+                    )
+                    console.log(newList)
+                }
+                return newList;
+            }
+            render() {
+                return (
+                    <div>
+                        <h1>列表渲染</h1>
+                        <p>{this.renderMain()}</p>    
+                    </div>
+                )
+            }
+        }
+        const element = <App/>
+        const container = document.getElementById("root");
+        const root = ReactDOM.createRoot(container)
+        root.render(element);
+    </script>
+</body>
+
+</html>