react.js every nth item add opening tag or closing tag

后端 未结 2 1181
深忆病人
深忆病人 2021-01-02 09:22

I\'m having trouble with this logic since react/jsx does not allow for non closing tags to be added to an array/child component. For example with bootstrap css I want to add

2条回答
  •  囚心锁ツ
    2021-01-02 10:08

    render() {
       const rows = array_chunk(this.props.columns, 4)
       return (
         {
           rows.map((row) => (
             
    { row.map((col) => (
    { col }
    )) }
    )) } ) }

    An example array_chunk (I recommend that you use lodash)

    module.exports = function chunks(arr, size) {
      if (!Array.isArray(arr)) {
        throw new TypeError('Input should be Array');
      }
    
      if (typeof size !== 'number') {
        throw new TypeError('Size should be a Number');
      }
    
      var result = [];
      for (var i = 0; i < arr.length; i += size) {
        result.push(arr.slice(i, size + i));
      }
    
      return result;
    };
    

提交回复
热议问题