简体   繁体   English

ReactJS:表达 API 和 ReactJS 结构限制每行显示数据

[英]ReactJS: Express API and ReactJS Structure Limit ShowData per Row

I want to do something that I can't figure how to do, In a row, I want to show 4 boxes from my Express Backend data, after that, I want to skip to another row and print more 4 boxes, and report that until I reach the end of the API.我想做一些我不知道该怎么做的事情,我想连续显示我的 Express 后端数据中的 4 个框,之后,我想跳到另一行并打印更多 4 个框,然后报告直到我到达 API 的末尾。

With Express I have created my API and it's small 114 (but I want to use 112 for this question) columns with data, So what I want to do is I want to print a row with 4 columns then report the same state again.使用 Express,我创建了我的 API,它很小,有 114 个(但我想为这个问题使用 112 个)列的数据,所以我想要打印一行有 4 个列,然后再次报告相同的 state。

Here is my code, but the problem is that it shows all the columns within one row (all 114 columns) and it gets messed up.这是我的代码,但问题是它显示了一行中的所有列(所有 114 列)并且它被搞砸了。

{this.state.BookData.map(BookData =>
    <a className="surah" href={BookData.link}>
        <p className="surah-counter">{BookData.id}</p>
        <div>
            <h2 className="custom-clax1">{BookData.sure}</h2>
            <h1 className="custom-clax2">{BookData.surah}</h1>
            <h3 className="custom-clax3">{BookData.number}</h3>
        </div>
    </a>)}

I can't figure out how to do it, to show the 4 columns per row.我不知道该怎么做,每行显示 4 列。

One quick way to do it would be by一种快速的方法是通过

{this.state.BookData.map((BookData, index) =>
  <>
    {(index + 1) % 4 == 0 && <br/>}
    <a className="surah" href={BookData.link}>
        <p className="surah-counter">{BookData.id}</p>
        <div>
            <h2 className="custom-clax1">{BookData.sure}</h2>
            <h1 className="custom-clax2">{BookData.surah}</h1>
            <h3 className="custom-clax3">{BookData.number}</h3>
        </div>
    </a>
  </>
)}

All this code does is insert a break tag <br/> every 4 items in the array BookData , breaking the long line of items into multiple lines.这段代码所做的只是在数组BookData中每 4 个项目插入一个中断标记<br/> ,将一长串项目分成多行。

Mainly the line {(index + 1) % 4 == 0 && <br/>} does that.主要是{(index + 1) % 4 == 0 && <br/>}行。

Now if would like to wrap every 4 elements into something, like a <div/> , you're gonna need to chunk the BookData array ( look at this question for that ) into chunks of 4, them loop through it like this, for example现在,如果想将每 4 个元素包装成一个东西,比如<div/> ,你需要将BookData数组( 看这个问题)分成 4 个块,它们像这样循环遍历它,对于例子

{chunckedBookData.map((bookDataChunk) =>
  <div class="custom-style-for-each-chunk">
    {bookDataChunk.map((BookData, index) =>
      <a className="surah" href={BookData.link}>
        <p className="surah-counter">{BookData.id}</p>
        <div>
            <h2 className="custom-clax1">{BookData.sure}</h2>
            <h1 className="custom-clax2">{BookData.surah}</h1>
            <h3 className="custom-clax3">{BookData.number}</h3>
        </div>
      </a>
    )}
  <div/>
)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM