I have an array of objects that I load from a json file and I want each item to load separately when I click a button. I completed that but the main problem is when the index goes out of bounds so then it shows
TypeError "Cannot read property 'content' of undefined".
How can I make then index be 0 again when the index reaches the last item? I tried solving it by including some if statements inside the button but no luck.
Here is the related part of the code till now:
function App() {
const classes = useStyles();
const questions_size = Data.length;
const [num, setNum] = useState(0);
const current = Data[num].content;
(...)
return (
<Container>
<Typography variant="h3" gutterBottom>
About
</Typography>
<Button variant="contained" color="primary" onClick={()=> num<questions_size?setNum(num+1): setNum(0)}>Επομενο</Button>
<Card className={classes.root}>
<CardContent>
<Typography variant="body1" gutterBottom>
<p style={{ fontFamily: 'Tangerine, serif', fontSize: '35px', textShadow: '4px 4px 4px #aaa'}}>{current}</p>
</Typography>
</CardContent>
</Card>
</Container>
)
Data?