filter vs map reactjs and jsx

后端 未结 3 527
慢半拍i
慢半拍i 2021-01-06 15:45

I\'m working on a react project to learn react.

In a component\'s render method, when I use .map to iterate over values and return an array of component

相关标签:
3条回答
  • 2021-01-06 16:09

    If you only want to use one pass over the array you can use reduce:

    books && books
    .reduce(
      (all,book, index) => {
        if (book.shelf !== shelf) {
          return all;
        }
        return all.concat(
          <Book
            key={book && book.id ? book.id : index}
            changeShelf={this.props.changeShelf}
            book={book} />
        );
      }
      ,[]
    )
    

    However; I think using filter and map makes for code that's easier to read.

    0 讨论(0)
  • 2021-01-06 16:13

    Array.filter does not allow you to transform the data into components. That is the job of Array.map.

    You should instead filter first, then chain the map call afterward:

    {              
      books && books
        .filter(book => book.shelf === shelf)
        .map((book, index) => {
          return (
            <Book
               key={book && book.id ? book.id : index}
               changeShelf={this.props.changeShelf}
               book={book} />
          );
        })
    }
    

    If you want to avoid a second pass over your list of books, you can return null as well, though this is "less good" because you're forcing React to render null when it doesn't need to do any work at all:

    {              
      books && books
        .map((book, index) => {
          if (book.shelf !== shelf) {
            return null;
          }
          return (
            <Book
               key={book && book.id ? book.id : index}
               changeShelf={this.props.changeShelf}
               book={book} />
          );
        })
    }
    
    0 讨论(0)
  • There is nothing unique to React and map() or filter().

    In the first example when using map() you are returning an array of React components which are rendered in the DOM. You are transforming (mapping) each plain JavaScript object in the array into a React component. As a matter of fact, you are also going to return some undefined elements in the resulting array, if the condition book.shelf === shelf is falsy. Your array may look like [<Book />, <Book />, undefined, <Book />, undefined]. That's not such a big deal, since React won't render falsy values (null or undefined elements will just be skipped).

    The second example won't return the same result (an array of React components), but an array of plain JavaScript objects (of type book). This is because no matter what are you returning from the filter function, it's going to be cast to a Boolean value - true or false and that value is going to decide if the current element is going to be filtered or not. The result of your .filter() function is going to be something like this (imagine shelf === 'Science'):

    Original array: [{ shelf: "Science" }, { shelf: "Thrillers" }, { shelf: "Informatics" }]
    Filtered array: [{ shelf: "Science" }]
    

    As you can see, the items in the array won't be React components (<Book />) and React won't be able to render them in the DOM, thus the error it throws.

    0 讨论(0)
提交回复
热议问题