美文网首页
jsx(5)-列表渲染

jsx(5)-列表渲染

作者: 未路过 | 来源:发表于2022-11-10 20:57 被阅读0次

    列表渲染

    image.png
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>计数器</title>
      <style>
        .item {
          margin: 10px;
          padding: 0 20px;
          border: 1px solid orange;
        }
      </style>
    </head>
    <body>
      
      <div id="root"></div>
    
      <script src="../lib/react.js"></script>
      <script src="../lib/react-dom.js"></script>
      <script src="../lib/babel.js"></script>
    
      <script type="text/babel">
        // 1.定义App根组件
        class App extends React.Component {
          constructor() {
            super()
            this.state = {
              students: [
                { id: 111, name: "why", score: 199 },
                { id: 112, name: "kobe", score: 98 },
                { id: 113, name: "james", score: 199 },
                { id: 114, name: "curry", score: 188 },
              ]
            }
          }
    
          render() {
            const { students } = this.state
    
            // 分数大于100的学生进行展示
            const filterStudents = students.filter(item => {
              return item.score > 100
            })
    
            // 分数大于100, 只展示两个人的信息
            // slice(start, end): [start, end)
            const sliceStudents = filterStudents.slice(0, 2)
    
            return (
              <div>
                <h2>学生列表数据</h2>
                <div className="list">
                  {
                    students.filter(item => item.score > 100).slice(0, 2).map(item => {
                      return (
                        <div className="item" key={item.id}>
                          <h2>学号: {item.id}</h2>
                          <h3>姓名: {item.name}</h3>
                          <h1>分数: {item.score}</h1>
                        </div>
                      )
                    })
                  }
                </div>
              </div>
            )
          }
        }
    
        // 2.创建root并且渲染App组件
        const root = ReactDOM.createRoot(document.querySelector("#root"))
        root.render(<App/>)
      </script>
    
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:jsx(5)-列表渲染

          本文链接:https://www.haomeiwen.com/subject/iqsvtdtx.html