zheng пре 18 часа
родитељ
комит
4c4c763be3
3 измењених фајлова са 136 додато и 1 уклоњено
  1. 1 1
      21.react/初阶/12.类组件.html
  2. 107 0
      21.react/初阶/13.类组件.html
  3. 28 0
      21.react/初阶/14.Clock.html

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

@@ -33,7 +33,7 @@
                 })
             }
             handleAdd() {
-                this.setState((val) => ({ sum: val.sum + 10 })
+                this.setState((val) => ({ sum: val.sum + 1 })
                 )
             }
             handleReduce() {

+ 107 - 0
21.react/初阶/13.类组件.html

@@ -0,0 +1,107 @@
+<!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,
+                isShow: true
+            }
+            // 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 + 1 })
+                )
+            }
+            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 userInfo={this.state.name} />
+                //     </div >
+                // )
+                return <Child userInfo={this.state.name} />
+            }
+        }
+        class Child extends React.Component {
+            constructor() {
+                console.log('con')
+                super();
+                this.state = {
+                    weather: "晴天"
+                }
+                this.handleWeather = this.handleWeather.bind(this);
+            }
+            // 挂载:constructor render componentDidMount
+            // 更新:render componentDidUpdate
+            componentDidMount() {
+                console.log("挂载")
+            }
+            componentDidUpdate() {
+                console.log("222")
+            }
+            componentWillUnMount() {
+                console.log("卸载")
+            }
+            handleWeather() {
+                this.setState({
+                    weather: "雨天"
+                })
+                console.log(this)
+            }
+            render() {
+                console.log("render")
+                return (
+                    <div>
+                        <h1>外面是{this.state.weather},我叫{this.props.userInfo}</h1>
+                        <button onClick={this.handleWeather}>修改天气</button>
+                    </div>
+                )
+            }
+        }
+        const element = <News />
+        const container = document.getElementById("root");
+        const root = ReactDOM.createRoot(container)
+        root.render(element);
+    </script>
+</body>
+
+</html>

+ 28 - 0
21.react/初阶/14.Clock.html

@@ -0,0 +1,28 @@
+<!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">
+        /**
+         * 页面展示当前时间
+         * 第一个按钮 点击停止
+         * 第二个按钮 点击继续
+        */
+        const element = <h1>你好</h1>
+        const container = document.getElementById("root");
+        const root = ReactDOM.createRoot(container)
+        root.render(element);
+    </script>
+</body>
+
+</html>