• React基础-JSX语法列表渲染详解


    React列表渲染

    真实开发中我们会从服务器请求到大量的数据,数据会以列表的形式存储:

    比如歌曲、歌手、排行榜列表的数据;

    比如商品、购物车、评论列表的数据;

    比如好友消息、动态、联系人列表的数据;

    在React中并没有像Vue模块语法中的v-for指令,而且需要我们通过JavaScript代码的方式组织数据,转成JSX:

    很多从Vue转型到React的小伙伴非常不习惯,认为Vue的方式更加的简洁明了;

    但是React中的JSX正是因为和JavaScript无缝的衔接,让它可以更加的灵活;

    另外我经常会提到React是真正可以提高我们编写代码能力的一种方式;

    如何展示列表呢?

    在React中,展示列表最多的方式就是使用数组的map高阶函数;

    class App extends React.Component {
      constructor() {
        super()
    
        this.state = {
          students: [
            {name: "aaa", age: 18, score: 99, id: 101},
            {name: "bbb", age: 19, score: 88, id: 102},
            {name: "ccc", age: 17, score: 77, id: 103},
            {name: "ddd", age: 17, score: 98, id: 104}
          ] 
        }
      }
    
      render() {
        const { students } = this.state
    
        return (
          <div>
            {
              students.map(stu => {
                return(
                  <div key={stu.id}>
                    <h3>学号: {stu.name}</h3>
                    <h3>年龄: {stu.age}</h3>
                    <h3>成绩: {stu.score}</h3>  
                  </div>
                )
              })
            }
          </div>
        )
      }
    }
    
    const app = ReactDOM.createRoot(document.querySelector("#app"))
    app.render(<App/>)
    

    很多时候我们在展示一个数组中的数据之前,需要先对它进行一些处理:

    比如过滤掉一些内容:filter函数; 例如上面代码中, 要求展示学生分数大于80的

    class App extends React.Component {
      constructor() {
        super()
    
        this.state = {
          students: [
            {name: "aaa", age: 18, score: 99, id: 101},
            {name: "bbb", age: 19, score: 88, id: 102},
            {name: "ccc", age: 17, score: 77, id: 103},
            {name: "ddd", age: 17, score: 98, id: 104}
          ] 
        }
      }
    
      render() {
        const { students } = this.state
    
        // 对数组过滤再遍历
        const filterStudents = students.filter(item => {
          return item.score > 80
        })
    
        return (
          <div>
            {
              filterStudents.map(stu => {
                return(
                  <div key={stu.id}>
                    <h3>学号: {stu.name}</h3>
                    <h3>年龄: {stu.age}</h3>
                    <h3>成绩: {stu.score}</h3>  
                  </div>
                )
              })
            }
          </div>
        )
      }
    }
    
    const app = ReactDOM.createRoot(document.querySelector("#app"))
    app.render(<App/>)
    

    比如截取数组中的一部分内容:slice函数, 例如上面代码中, 要求截取数组的前两条进行展示

    class App extends React.Component {
      constructor() {
        super()
    
        this.state = {
          students: [
            {name: "aaa", age: 18, score: 99, id: 101},
            {name: "bbb", age: 19, score: 88, id: 102},
            {name: "ccc", age: 17, score: 77, id: 103},
            {name: "ddd", age: 17, score: 98, id: 104}
          ] 
        }
      }
    
      render() {
        const { students } = this.state
        
        // 对数组截取再遍历
        const spliceStudents = students.splice(0,2)
    
        return (
          <div>
            {
              spliceStudents.map(stu => {
                return(
                  <div key={stu.id}>
                    <h3>学号: {stu.name}</h3>
                    <h3>年龄: {stu.age}</h3>
                    <h3>成绩: {stu.score}</h3>  
                  </div>
                )
              })
            }
          </div>
        )
      }
    }
    
    const app = ReactDOM.createRoot(document.querySelector("#app"))
    app.render(<App/>)
    

    上面的操作先过滤, 再截取, 最后进行遍历, 其实可以通过链式调用一行代码完成

    class App extends React.Component {
          constructor() {
            super()
    
            this.state = {
              students: [
                {name: "aaa", age: 18, score: 99, id: 101},
                {name: "bbb", age: 19, score: 88, id: 102},
                {name: "ccc", age: 17, score: 77, id: 103},
                {name: "ccc", age: 17, score: 77, id: 103},
                {name: "ddd", age: 17, score: 98, id: 104}
              ] 
            }
          }
    
          render() {
            const { students } = this.state
    
            return (
              <div>
                {/* 链式调用, 一步操作完成 */}
                {
                  students.filter(item => item.score > 80).splice(0, 2).map(stu => {
                    return(
                      <div key={stu.id}>
                        <h3>学号: {stu.name}</h3>
                        <h3>年龄: {stu.age}</h3>
                        <h3>成绩: {stu.score}</h3>  
                      </div>
                    )
                  })
                }
              </div>
            )
          }
        }
        
        const app = ReactDOM.createRoot(document.querySelector("#app"))
        app.render(<App/>)
    

    我们会发现在前面的代码中如果没有绑定key就会会报一个警告, 这个警告是告诉我们需要在列表展示的jsx中添加一个key

    key主要的作用是为了提高diff算法时的效率;

  • 相关阅读:
    Hive【Hive(五)函数-高级聚合函数、炸裂函数】
    MCR3516与MCR3512读写器在麒麟系统中info.plist文件冲突解决方案
    Java --- Spring6前的程序问题
    【配置教程】撑起月6亿PV开源监控解决方案
    Transfer Learning with MobileNetV2(吴恩达课程)
    leetcode-07-[344]反转字符串[541]反转字符串II[卡码网54]替换数字
    零基础写框架(2):故障排查和日志基础
    搭建nginx https 反向代理 http tomcat服务实践。
    java毕业设计论文题目基于SSM实现的小区物业管理系统[包运行成功]
    流程引擎概述及组成
  • 原文地址:https://blog.csdn.net/m0_71485750/article/details/126602383