I have a test application that essentially gives a set of questions and shows one question after another when you press the next button. Each time the stack of questions becomes one less. I have an issue with state and the state-consuming component rendering in unexpected ways... I am using the useReducer hook and useContext but for the provided code I am just going to post the reducer as I feel like that is the main problem. When I press the button that dispatches to the "NEXT_QUESTION" action it works the FIRST TIME and the component re-renders with a new question, but any time after that the component ONLY re-renders by pressing the next button twice. Why do i have to press the next button two times to update my state? I can tell that the state is in fact updating as expected but I am not sure why it takes two button presses any time after the first one. here is my reducer... the console log is coming up every time. The deck is the array of questions
const deck=shuffler(deckFinal);
type State = {
deck:QuestionType[],
question:QuestionType,
score:number,
}
type Action={
type:'NEXT_QUESTION',
payload:string
}
export const questionReducer=(state: State, action: Action)=>{
switch(action.type){
case 'NEXT_QUESTION':
console.log('next')
const newState={
deck:state.deck.filter(item=>item.id!==action.payload),
question:state.deck[state.deck.length-1],
score:70
}
return newState;
}
switch(action.type){
case 'RESET_TEST':
//add shuffler function here
return action.payload
}
}