zheng 7 時間 前
コミット
090e8faaff
2 ファイル変更53 行追加3 行削除
  1. 50 0
      19.React/初阶/10.setState.html
  2. 3 3
      19.React/初阶/9.生命周期.html

+ 50 - 0
19.React/初阶/10.setState.html

@@ -0,0 +1,50 @@
+<!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">
+        // setState 更新组件状态state 触发组件重新渲染的核心方法
+        let root = ReactDOM.createRoot(document.getElementById("root"));
+        class Count extends React.Component {
+            constructor(props) {
+                super(props)
+                this.state = {
+                    count: 0
+                }
+            }
+            handleClick = () => {
+                // this.setState({
+                //     count: 2
+                // })
+                this.setState((state,props) => ({
+                    count: state.count + 1
+                }))
+            }
+            render() {
+                return (
+                    <div>
+                        <h1 onClick={this.handleClick}>{this.state.count}</h1>
+                    </div>
+                )
+            }
+        }
+        // function Count() {
+        //     return (
+        //         <div><h1></h1></div>
+        //     )
+        // }
+        root.render(<Count />);
+    </script>
+</body>
+
+</html>

+ 3 - 3
19.React/初阶/9.生命周期.html

@@ -33,10 +33,10 @@
             componentDidMount() {
                 console.log("挂载");
                 this.timer = setInterval(() => {
-                    this.setState((state, props) => {
-                        currentTime: new Date();
+                    this.setState((state, props) => ({
+                        currentTime: new Date(),
                         count: 2
-                    })
+                    }))
                 }, 1000)
             }
             // 更新