How to render react components by using map and join?

后端 未结 14 2220
南旧
南旧 2020-12-07 15:30

I have one component which is going to display Array of String. The code looks like this:

React.createClass({
  render() {
     
this.props
14条回答
  •  余生分开走
    2020-12-07 15:48

    The accepted answer actually returns an array of arrays because prev will be an array each time. React is smart enough to make this work, but is it is prone to causing problems in the future such as breaking Reacts diffing algorithm when giving keys to each result of map.

    The new React.Fragment feature lets us do this in an easy to understand manner without the underlying issues.

    class List extends React.Component {
      render() {
         
    {this.props.data .map((t, index) => {t} , )
    } }

    With React.Fragment we can simply place the separator , outside of the of the returned HTML and React won't complain.

提交回复
热议问题