I have an app like this
function App(){
const [appState, setAppState] = useState(
{
questions: []
}
)
const addToQuestion = (questionObject) =>{
setAppState((prevState) => {
return {...prevState, questions: [...prevState.questions, questionObject]}
})
}
let removeFromQuestionArray = () => {
setAppState((prevState) => {
let a = prevState.questions
a.pop()
return {...prevState, questions: a}
})
}
const onBackButtonClicked = () => {
removeFromQuestionArray()
}
}
But when I call removeFromQuestionsArray()
the setAppState works unpredictably. It sometimes removes all the items in the array even though I'm only popping the last item in the array. What could be causing this. I just want to remove the last item from the array, how can I go about this without having issues?