簡體   English   中英

react.js 每第 n 個項目添加開始標簽或結束標簽

[英]react.js every nth item add opening tag or closing tag

我在這個邏輯上遇到了問題,因為 react/jsx 不允許將非結束標記添加到數組/子組件中。 例如,對於 bootstrap css,我想為4 列添加一行。

所以邏輯如下:

添加一個開頭的行 ex: <div className="row"> ,然后在該行內循環,每個循環附加一個列 ex: <div className="column>{this.data}</div>當循環達到 4 檢查使用if(i % 4 == 0)並添加結束</div>標記,同時添加新行標記<div className="row">

work in another language but in react this is not doable since we push a closing tag and a opening tag (which is invalid jsx):下面的代碼用另一種語言工作,但在 React 中這是不可行的,因為我們推送了一個結束標簽和一個開始標簽(這是無效的 jsx):

generateColumns(columns) {
 let newColumns = [];

 columns.forEach(function(column, idx) {
  newColumns.push( <div className="column"> some data </div> );

  if (idx % 4 == 0) {
   // Here we end the row and start a new row, works in any other language.
   newColumns.push( </div> <div className="row"> );
  }
 });

 // This array now has the proper tags for opening a row every 4th item and closing it.
 return newColumns;
},
render() {
   return (
     <div className="row">
       {this.generateColumns(this.props.columns)}
     </div>
   )
}

預期輸出將是:

<div class="row">
  <div class="column">
   Some data
  </div>
  <div class="column">
   Some more data
  </div>
  <div class="column">
   Other data
  </div>
  <div class="column">
   Something else
  </div>
</div>
<div class="row">
  <div class="column">
   Some data
  </div>
  <div class="column">
   Some more data
  </div>
  <div class="column">
   Other data
  </div>
  <div class="column">
   Something else
  </div>
</div>

//the above would be repeated and new rows would appear every 4 columns.

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

一個示例 array_chunk(我建議你使用 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;
};

我實際上只是使用了數組,並且反應很好地處理了渲染。

render() {
    let rows = [],
        cols = []
    let index = 0
    const totalCols = 20;

    for (index; index < totalCols; index++) {
        cols.push(<div class="col" key={index}/>)
        if ((index + 1) % 4 == 0) {
            rows.push(
                <div class="row" key={index}>
                    {cols}
                </div>
            )
            cols = []
        }
    }
    return (
        <div class="container">
            {rows}
        </div>
    )
}

在中使用.map<div> 標簽-react.js</div><div id="text_translate"><p> 我真的很感激任何幫助。 所以該項目是一個反應項目。</p><p> 這是我獲取<a href="https://i.stack.imgur.com/WelCP.png" rel="nofollow noreferrer">的內容:獲取請求</a></p><p>這是我從服務器響應的內容: <a href="https://i.stack.imgur.com/e2QG3.png" rel="nofollow noreferrer">Response from server</a></p><p> 我試了一下 Postman,看看我收到的是什么類型的 json。 這是它返回的內容: <a href="https://i.stack.imgur.com/yg6IE.png" rel="nofollow noreferrer">Postman 測試</a></p><p>我遇到的問題是我無法 map 收到的數據。 一旦輸入鏈接,我只想在首頁上以一種很好的方式顯示接收到的數據。</p><p> 是否可以將 map 放在 div 標簽內? 或者還有另一種方法嗎?</p><p> <em><strong>請注意該項目是作為功能組件編寫的</strong></em></p></div>

[英]Using .map in <div> tag - react.js

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM