zheng 14 часов назад
Родитель
Сommit
2602ad5627
2 измененных файлов с 77 добавлено и 0 удалено
  1. 1 0
      21.react/初阶/11.事件.html
  2. 76 0
      21.react/初阶/12.类组件.html

+ 1 - 0
21.react/初阶/11.事件.html

@@ -36,6 +36,7 @@
             console.log("222")
             console.log("222")
             // event.stopPropagation();
             // event.stopPropagation();
         }
         }
+        // function han
         const handleJump = (event) => {
         const handleJump = (event) => {
             console.log("da'yin")
             console.log("da'yin")
             event.preventDefault();
             event.preventDefault();

+ 76 - 0
21.react/初阶/12.类组件.html

@@ -0,0 +1,76 @@
+<!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>
+    <script type="text/babel">
+        // props
+        // state
+        class News extends React.Component {
+            state = {
+                name: '图图',
+                age: 3,
+                sum: 0
+            }
+            // handleClick = () => {
+            //     console.log("触发")
+            //     // this.state.age += 2;
+            //     console.log(this)
+            // }
+            handleClick() {
+                this.setState({
+                    age: this.state.age + 10,
+                    name: 'Tom'
+                })
+            }
+            handleAdd() {
+                this.setState((val) => ({ sum: val.sum + 10 })
+                )
+            }
+            handleReduce() {
+                this.setState({
+                    sum: this.state.sum - 1
+                })
+            }
+            render() {
+                console.log(this, '11')
+                return (
+                    <div>
+                        <h1>父组件</h1>
+                        <h1>数值:{this.state.sum}</h1>
+                        <button onClick={this.handleAdd.bind(this)}>+1</button>
+                        <button onClick={this.handleReduce.bind(this)}>-1</button>
+                        <h3>我叫{this.state.name},今年{this.state.age}岁</h3>
+                        <button onClick={this.handleClick.bind(this)}>+2</button>
+                        <button onClick={() => {
+                            console.log(this)
+                        }}>+2</button>
+                        <Child />
+                    </div >
+                )
+            }
+        }
+        class Child extends React.Component {
+            render() {
+                return (
+                    <div>子组件</div>
+                )
+            }
+        }
+        const element = <News />
+        const container = document.getElementById("root");
+        const root = ReactDOM.createRoot(container)
+        root.render(element);
+    </script>
+</body>
+
+</html>