const [currentIndexes, setCurrentIndexes] = useState([1,2,3,4]);
let displayCreditorsData = currentIndexes.map((e) => allCreditorsData[e]);
const totalCreditors = allCreditorsData.length;
function nextCards() {
let indexes=[];
currentIndexes.map((position, index) => {
if (position === allCreditorsData.length) {
return indexes.push((position = 0));
} else {
return indexes.push(position + 1);
}
});
setCurrentIndexes(indexes);
displayCreditorsData = (indexes.map((e) => allCreditorsData[e]));
}
function prevCards() {
let indexes = [];
currentIndexes.map((position, index) => {
if (position === 0) {
return indexes.push((position = totalCreditors - 1));
} else {
return indexes.push(position - 1);
}
});
setCurrentIndexes(indexes);
displayCreditorsData = (indexes.map((e) => allCreditorsData[e]));
}
I have the above code which lets me render some data in cards. If I have 4 or more than 4 data, I dont have any issue. But when I have less than 4, I get the extra card which is just the template of the card design. I tried to limit currentIndexes state to [1], but that than only displays 1 card in the space which can accomodate 4 in total.
I checked other solutions but they were about just rendering the data in cards, and couldn’t address my problem properly. How do I solve this issue?
2
Answers
Defined states on top so that we can use each of them in any place we want without having to worry about getting not defined error
and finally we get the chunks from the array we receive into displayCreditorsData, which is then used within return<></> as needed.
for nextCard and prevCard card function @Anshu solution is perfect.
Issue
When allCreditorsData array size is less than 4, else block of
nextCards
andprevCards
leads to pushing unwanted index to indexes variable and further goes to dispalyCreditorsData.Solution
Used two useEffect.
First useEffect -> To fetch the data and to set the currentIndexes when YourComponent renders for the first time
Second useEffect -> To set display data when any of the variables change from
[allCreditorsData, currentIndexes]
Added 1000ms delay to mimic asynchronous operation.
Logic of event handler
In nextCards function`(index + 1) % totalCreditors. It increments the index by 1 and then takes the modulo wrt totalCreditors. If the index reaches the end of the array, the modulo will return 0. Similarly, you can think for prevCards function
If you wanna play around with code.