I am using react ultimate pagination
to make a pagination functionality. When a page number is clicked I am updating a variable to show what is needed. The problem is that I have a loop which can have variable number of elements, so I do not know how many of them are there in advance. Here is the code:
{
this.state.fullAnalysisDone && this.state.activeKey == 3 ?
<div>
{
this.get_analysis_charts(this.chartTypes[parseInt(this.state.activeKey)]).map((chart_arr, idx) => {
return <div style={{visibility: this.state.currPageDiffGenes === (idx + 1) ? 'visible' : 'hidden' }}>
<img src = {chart_arr[0]} style = {{height:"400px", width:"500px"}}/>
<img src = {chart_arr[1]} style = {{height:"400px", width:"500px"}}/>
</div>
})
}
<div style = {{marginLeft: '35%'}}>
<UltimatePaginationBootstrap4 key = 'diff_genes_pagination'
totalPages = {this.get_analysis_charts(this.chartTypes[parseInt(this.state.activeKey)]).length}
onChange = {this.changedDiffGenes}
currentPage = {this.state.currPageDiffGenes}/>
</div>
</div>
: ""
}
I can not really leave here <div style={{visibility: this.state.currPageDiffGenes === (idx + 1) ? 'visible' : 'hidden' }}>
because it takes space on the page. If I knew beforehand the number of iterations, I could have just done the following:
{
this.state.currPageDiffGenes === 1 ?
loop over but return only the 1st element
:""
}
{
this.state.currPageDiffGenes === 2 ?
loop over but return only the 2nd element
:""
}
...
And it would work because elements would be recreated each time, but I can not do it with variable length loop. How could we solve this issue? I am using d3
in the app, so I can just assign ids
to the respective divs
, and maybe destroy-insert elements while using pagination, but I feel like that is overkill and there should be an easier solution.