郑柏铃 1 napja
szülő
commit
aff0a6210f

+ 78 - 0
20.react/初阶/16.条件渲染.html

@@ -0,0 +1,78 @@
+<!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 root = ReactDOM.createRoot(document.getElementById("root"));
+        /**
+         * 条件判断:
+         * if/else/else-if
+         * switch-case
+         * 三元判断
+        */
+        function App() {
+            return (
+                <div>
+                    <News />
+                </div>
+            )
+        }
+        class News extends React.Component {
+            constructor() {
+                super();
+                this.state = {
+                    msg: {
+                        user: "图图",
+                        age: 3
+                    },
+                    token: JSON.parse(localStorage.getItem("token"))
+                }
+            }
+            handleLogin() {
+                localStorage.setItem('token',JSON.stringify('haha'));
+                this.setState({
+                    token:'haha'
+                })
+            }
+            render() {
+                let { token, msg } = this.state;
+                console.log(token, msg)
+                // 逻辑: 如果存在 token 显示已登录; 不存在 显示 请登录
+                // if (token) {
+                //     return (
+                //         <div>
+                //             已登录
+                //         </div>
+                //     )
+                // } else {
+                //     return (
+                //         <div>
+                //             请登录
+                //         </div>
+                //     )
+                // }
+                return token ? (
+                    <h1>已登录</h1>
+                ) : (
+                    <div>
+                        <h2>请登录</h2>
+                        <button onClick={this.handleLogin.bind(this)}>请登录</button>    
+                    </div>
+                )
+            }
+        }
+        root.render(<App />);
+    </script>
+</body>
+
+</html>

+ 55 - 0
20.react/初阶/17.循环渲染.html

@@ -0,0 +1,55 @@
+<!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 root = ReactDOM.createRoot(document.getElementById("root"));
+        function App() {
+            return (
+                <div>
+                    <List/>
+                </div>
+            )
+        }
+        class List extends React.Component {
+            constructor() {
+                super();
+                this.state = {
+                    list:[1,2,3,4]
+                }
+            }
+            render() {
+                let {list} = this.state;
+                let l1 = list.map((item,index) => <li key={index}>{item}</li>)
+                return (
+                    <div>
+                        {/*
+                            <ul>
+                                <li>1</li>
+                                <li>2</li>
+                                <li>3</li>
+                                <li>4</li>
+                            </ul>       
+                        */} 
+                        <ul>
+                            {l1}
+                        </ul>
+                    </div>
+                )
+            }
+        }
+        root.render(<App />);
+    </script>
+</body>
+
+</html>

+ 7 - 2
20.react/初阶/模版.html

@@ -1,5 +1,6 @@
 <!DOCTYPE html>
 <html lang="en">
+
 <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
@@ -8,16 +9,20 @@
     <script src="./react.development.js"></script>
     <script src="./react-dom.development.js"></script>
 </head>
+
 <body>
     <div id="root"></div>
     <script type="text/babel">
         const root = ReactDOM.createRoot(document.getElementById("root"));
         function App() {
             return (
-                <div></div>
+                <div>
+                </div>
             )
         }
-        root.render(<App/>);
+
+        root.render(<App />);
     </script>
 </body>
+
 </html>

+ 204 - 0
20.react/初阶/表情选择.html

@@ -0,0 +1,204 @@
+<!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 root = ReactDOM.createRoot(document.getElementById("root"));
+    function App() {
+      return (
+        <div>
+          <EmojiPicker />
+        </div>
+      )
+    }
+    class EmojiPicker extends React.Component {
+      constructor(props) {
+        super(props);
+        this.state = {
+          // 选中的表情索引,-1表示未选择
+          selectedIndex: -1,
+          // 悬停的表情索引,-1表示无悬停
+          hoverIndex: -1
+        };
+
+        // 绑定事件处理函数
+        this.handleSelect = this.handleSelect.bind(this);
+        this.handleHover = this.handleHover.bind(this);
+        this.handleLeave = this.handleLeave.bind(this);
+        this.clearSelection = this.clearSelection.bind(this);
+      }
+
+      // 表情数据
+      emojis = [
+        { symbol: '😊', name: '开心' },
+        { symbol: '😍', name: '爱慕' },
+        { symbol: '😂', name: '大笑' },
+        { symbol: '😢', name: '难过' },
+        { symbol: '😠', name: '生气' },
+        { symbol: '😲', name: '惊讶' },
+        { symbol: '😴', name: '困倦' },
+        { symbol: '🤔', name: '思考' }
+      ];
+
+      // 选择表情
+      handleSelect(index) {
+        this.setState({
+          selectedIndex: index
+        });
+      }
+
+      // 鼠标悬停表情
+      handleHover(index) {
+        this.setState({
+          hoverIndex: index
+        });
+      }
+
+      // 鼠标离开表情
+      handleLeave() {
+        this.setState({
+          hoverIndex: -1
+        });
+      }
+
+      // 清除选择
+      clearSelection() {
+        this.setState({
+          selectedIndex: -1
+        });
+      }
+
+      render() {
+        const { selectedIndex, hoverIndex } = this.state;
+        const selectedEmoji = selectedIndex !== -1 ? this.emojis[selectedIndex] : null;
+
+        return (
+          <div style={containerStyle}>
+            <h2 style={titleStyle}>表情选择器</h2>
+
+            {/* 表情选择区域 */}
+            <div style={emojiGridStyle}>
+              {this.emojis.map((emoji, index) => (
+                <div
+                  key={index}
+                  onClick={() => this.handleSelect(index)}
+                  onMouseEnter={() => this.handleHover(index)}
+                  onMouseLeave={this.handleLeave}
+                  style={{
+                    ...emojiItemStyle,
+                    // 选中或悬停时的样式
+                    transform: (selectedIndex === index || hoverIndex === index)
+                      ? 'scale(1.2)'
+                      : 'scale(1)',
+                    backgroundColor: selectedIndex === index ? '#e3f2fd' : 'transparent',
+                    borderColor: selectedIndex === index ? '#2196F3' : '#eee'
+                  }}
+                >
+                  <span style={emojiSymbolStyle}>{emoji.symbol}</span>
+                  <span style={emojiNameStyle}>{emoji.name}</span>
+                </div>
+              ))}
+            </div>
+
+            {/* 选择结果 */}
+            {selectedEmoji && (
+              <div style={selectionResultStyle}>
+                <p>你选择了:{selectedEmoji.symbol} {selectedEmoji.name}</p>
+                <button
+                  onClick={this.clearSelection}
+                  style={clearButtonStyle}
+                >
+                  清除选择
+                </button>
+              </div>
+            )}
+
+            {!selectedEmoji && (
+              <p style={instructionStyle}>请点击一个表情来表达你的心情</p>
+            )}
+          </div>
+        );
+      }
+    }
+
+    // 样式定义
+    const containerStyle = {
+      maxWidth: '600px',
+      margin: '20px auto',
+      padding: '20px',
+      textAlign: 'center',
+      fontFamily: 'Arial, sans-serif',
+      border: '1px solid #e0e0e0',
+      borderRadius: '10px',
+      boxShadow: '0 2px 8px rgba(0,0,0,0.1)'
+    };
+
+    const titleStyle = {
+      color: '#333',
+      marginBottom: '25px'
+    };
+
+    const emojiGridStyle = {
+      display: 'grid',
+      gridTemplateColumns: 'repeat(4, 1fr)',
+      gap: '15px',
+      marginBottom: '25px'
+    };
+
+    const emojiItemStyle = {
+      padding: '15px 10px',
+      border: '1px solid #eee',
+      borderRadius: '8px',
+      cursor: 'pointer',
+      transition: 'all 0.2s ease',
+    };
+
+    const emojiSymbolStyle = {
+      fontSize: '32px',
+      display: 'block',
+      marginBottom: '5px'
+    };
+
+    const emojiNameStyle = {
+      fontSize: '14px',
+      color: '#666'
+    };
+
+    const selectionResultStyle = {
+      padding: '15px',
+      backgroundColor: '#f8f9fa',
+      borderRadius: '8px',
+      marginBottom: '10px'
+    };
+
+    const clearButtonStyle = {
+      padding: '6px 12px',
+      marginTop: '10px',
+      backgroundColor: '#f44336',
+      color: 'white',
+      border: 'none',
+      borderRadius: '4px',
+      cursor: 'pointer',
+      fontSize: '14px'
+    };
+
+    const instructionStyle = {
+      color: '#666',
+      fontStyle: 'italic'
+    };
+
+    root.render(<App />);
+  </script>
+</body>
+
+</html>

+ 183 - 0
20.react/初阶/颜色选择器.html

@@ -0,0 +1,183 @@
+<!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 root = ReactDOM.createRoot(document.getElementById("root"));
+    function App() {
+      return (
+        <div>
+          <ColorSwitcherContainer />
+        </div>
+      )
+    }
+
+    // 纯函数组件 - 负责UI展示
+    function ColorSwitcher(props) {
+      const { currentColor, colors, onColorClick } = props;
+
+      return (
+        <div style={containerStyle}>
+          <h2 style={titleStyle}>颜色点击切换器</h2>
+
+          {/* 颜色展示区 */}
+          <div
+            style={{
+              ...colorDisplayStyle,
+              backgroundColor: currentColor.code
+            }}
+          >
+            <span style={colorNameStyle}>
+              {currentColor.name}
+            </span>
+            <span style={colorCodeStyle}>
+              {currentColor.code}
+            </span>
+          </div>
+
+          {/* 颜色选择区 */}
+          <div style={colorSelectorStyle}>
+            {colors.map((color, index) => (
+              <button
+                key={index}
+                onClick={() => onColorClick(index)}
+                style={{
+                  ...colorButtonStyle,
+                  backgroundColor: color.code,
+                  border: currentColor.code === color.code ? '3px solid #333' : 'none'
+                }}
+                title={color.name}
+              />
+            ))}
+          </div>
+
+          <p style={instructionStyle}>点击下方颜色块切换颜色</p>
+        </div>
+      );
+    }
+
+    // 类组件 - 负责状态管理
+    class ColorSwitcherContainer extends React.Component {
+      constructor(props) {
+        super(props);
+
+        // 颜色数据
+        this.colors = [
+          { name: '红色', code: '#ff4444' },
+          { name: '绿色', code: '#00C851' },
+          { name: '蓝色', code: '#33b5e5' },
+          { name: '黄色', code: '#ffbb33' },
+          { name: '紫色', code: '#aa66cc' },
+          { name: '橙色', code: '#ff9900' },
+          { name: '粉色', code: '#ff4444' },
+          { name: '青色', code: '#00ffff' }
+        ];
+
+        this.state = {
+          currentIndex: 0 // 默认选中第一个颜色
+        };
+
+        // 绑定事件处理函数
+        this.handleColorClick = this.handleColorClick.bind(this);
+      }
+
+      // 处理颜色点击
+      handleColorClick(index) {
+        this.setState({
+          currentIndex: index
+        });
+      }
+
+      render() {
+        const currentColor = this.colors[this.state.currentIndex];
+
+        return (
+          <ColorSwitcher
+            currentColor={currentColor}
+            colors={this.colors}
+            onColorClick={this.handleColorClick}
+          />
+        );
+      }
+    }
+
+    // 样式定义
+    const containerStyle = {
+      maxWidth: '500px',
+      margin: '20px auto',
+      padding: '20px',
+      textAlign: 'center',
+      fontFamily: 'Arial, sans-serif'
+    };
+
+    const titleStyle = {
+      color: '#333',
+      marginBottom: '20px'
+    };
+
+    const colorDisplayStyle = {
+      width: '100%',
+      height: '150px',
+      borderRadius: '8px',
+      marginBottom: '25px',
+      display: 'flex',
+      flexDirection: 'column',
+      justifyContent: 'center',
+      alignItems: 'center',
+      transition: 'background-color 0.3s ease'
+    };
+
+    const colorNameStyle = {
+      fontSize: '24px',
+      fontWeight: 'bold',
+      color: 'white',
+      textShadow: '0 1px 2px rgba(0,0,0,0.5)',
+      margin: '0 0 5px 0'
+    };
+
+    const colorCodeStyle = {
+      fontSize: '16px',
+      color: 'white',
+      textShadow: '0 1px 2px rgba(0,0,0,0.5)',
+      margin: 0
+    };
+
+    const colorSelectorStyle = {
+      display: 'flex',
+      flexWrap: 'wrap',
+      gap: '10px',
+      justifyContent: 'center',
+      marginBottom: '20px'
+    };
+
+    const colorButtonStyle = {
+      width: '50px',
+      height: '50px',
+      borderRadius: '50%',
+      cursor: 'pointer',
+      transition: 'transform 0.2s ease',
+      outline: 'none',
+      '&:hover': {
+        transform: 'scale(1.1)'
+      }
+    };
+
+    const instructionStyle = {
+      color: '#666',
+      fontSize: '14px'
+    };
+    root.render(<App />);
+  </script>
+</body>
+
+</html>