I'm trying to build an app in React that will render interview questions from HTML, CSS, JS and React. I've set up a data.js file that is an array with objects inside that hold the Id, language, question and answer.
I've set up buttons for PreviousQuestion, NextQuestion and RandomQuestion.
I'm having trouble writing code for the PreviousQuestion, NextQuestion button. I've tried the following code on both but it doesn't work properly:
const questions = data.map((item) => item.question);
class App extends React.Component {
state = {
question: "",
count: 0,
};
prevQuestion = () => {
this.setState({
count: this.state.count - 1,
question: questions[this.state.count]
});
};
nextQuestion = () => {
this.setState({
count: this.state.count + 1,
question: questions[this.state.count]
});
I press nextQuestion this.state.count is 0 then 1 then 2 and then I press PrevQuestion and this.state.count goes to 3 and only then back to 2 1 ...
Can anyone point me in the right direction on how to solve this so it always increments and decrements properly please?