React Basics - Detailed Explanation of JSX Syntax List Rendering

React list rendering

In real development, we will request a large amount of data from the server, and the data will be stored in the form of a list:

Data such as songs, artists, and chart lists;

Such as product, shopping cart, review list data;

Such as friend messages, news, contact list data;

In React, there is no v-for directive like the Vue module syntax, and we need to organize data through JavaScript code and convert it into JSX :

Many small partners who have transformed from Vue to React are very unaccustomed, and think that the way of Vue is more concise and clear;

But JSX in React is more flexible because of its seamless connection with JavaScript;

Also I often mention React as a way to really improve our ability to write code;

How to display the list?

In React, the most common way to display lists is to use arrays 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/>)

Many times before we display the data in an array, we need to do some processing on it :

For example, to filter out some content: filter function; for example, in the above code, it is required to display students whose scores are greater than 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/>)

For example, to intercept a part of the array: slice function, for example, in the above code, it is required to intercept the first two items of the array for display

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/>)

The above operation is filtered first, then intercepted, and finally traversed. In fact, it can be completed by chaining a line of code.

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/>)

We will find that if there is no key binding in the previous code, a warning will be reported. This warning tells us that we need to add a key to the jsx displayed in the list

The main function of the key is to improve the efficiency of the diff algorithm;

Guess you like

Origin blog.csdn.net/m0_71485750/article/details/126602383