列表渲染示例

2025年11月29日 作者:管理员

示例代码

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>React 列表渲染</title>
  <script crossorigin src="https://unpkg.com/react@18.3.1/umd/react.production.min.js"></script>
  <script crossorigin src="https://unpkg.com/react-dom@18.3.1/umd/react-dom.production.min.js"></script>
  <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
  <style>
    body { font-family: system-ui, sans-serif; padding: 20px; }
    ul { list-style: none; padding: 0; }
    li { padding: 8px; margin: 5px 0; background: #f0f0f0; border-radius: 4px; }
  </style>
</head>
<body>
  <div id="root"></div>
  <script type="text/babel">
    function TodoList() {
      const [todos, setTodos] = React.useState([
        { id: 1, text: '学习 React' },
        { id: 2, text: '构建应用' },
        { id: 3, text: '分享知识' }
      ]);

      return (
        <ul>
          {todos.map(todo => (
            <li key={todo.id}>{todo.text}</li>
          ))}
        </ul>
      );
    }

    const root = ReactDOM.createRoot(document.getElementById('root'));
    root.render(<TodoList />);
  </script>
</body>
</html>