In my React app I am getting the following error: "Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops". I changed the state once the for loop is completed. I dont see what I am doing wrong. Any help will be appreciated.
//React
class Pagination extends Component {
state = {
li: []
}
liTags = (pageNum) =>{
return( <li className="PageNumber" key={pageNum}><span>{pageNum}</span></li>)
}
pagincationScript = (totalPages, page) =>{
let li = []
for(let i = 1; i < totalPages; i++){
li.push(this.liTags(i))
}
this.setState({
li: li
})
if(page > 1 && page < totalPages){
return(
<React.Fragment>
<li className="Btn Previous"><span><i>←</i>Previous</span></li>
{this.state.li}
<li className="Btn Previous"><span><i>→</i>Next</span></li>
</React.Fragment>
)
}
}
render() {
return (
<div className="Pagination">
<ul>
{this.pagincationScript(10,5)}
</ul>
</div>
)
}
}
export default PagePagination;