How to render react components by using map and join?

后端 未结 14 2210
南旧
南旧 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:53

    function YourComponent(props) {
    
      const criteria = [];
    
      if (something) {
        criteria.push({ something });
      }
    
      // join the jsx elements with `, `
      const elements = criteria.reduce((accu, elem) => {
        return accu === null ? [elem] : [...accu, ', ', elem]
      }, null);
    
      // render in a jsx friendly way
      return elements.map((el, index) => { el } );
    
    }
    

提交回复
热议问题