The calculateTotalPages function below calculates the total pages that will be displayed. Initial value for totalPages state is set to 0. Where should I call setState to update the totalPages state once the calculateTotalPages function has calculated and returned the value of total pages? I know setState is not to be called within render().
class Items extends Component {
state = {
items: ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7'],
isLoading: false,
currentPage: 1,
itemsPerPage: 3,
totalPages: 0
}
calculateTotalPages = (items, itemsPerPage) =>{
let getTotalPages;
const quotient = Math.trunc(items.length / itemsPerPage);
const remainder = items.length % itemsPerPage;
if(remainder === 0){
getTotalPages = quotient;
}else{
getTotalPages = quotient + remainder;
}
return getTotalPages;
};
render() {
const{items, totalPages, currentPage, itemsPerPage} = this.state;
const getTotalPages = this.calculateTotalPages(items, itemsPerPage)
console.log(getTotalPages)
return (
<div><h1>{getTotalPages}</h1></div>
)
}
}
export default Items;
componentDidMountif possible, and if it only needs to be set onceitemsanditemsPerPagestate values.