Okay so it's simple
I have an array of answers inside an array of questions. the user has the option to select more than one answer.
When an answer is selected, the text should change to selected and unselected if it isn't selected.
These are the steps i've tried to update my state
step 1 using map
setTestInfo((state) => { const allStateQuestions = state.info.questions; const currentQuestion = allStateQuestions.filter( (question) => question.id === questionId )[0]; const allAnswersMap = currentQuestion.answers.map((answer) => answer.id === answerId ? (() => { answer.is_chosen = !answer.is_chosen; return answer; })() : answer ); currentQuestion.answers = allAnswersMap; return { ...state, info: { ...state.info, questions: allStateQuestions, }, }; }); step 2 using find
setTestInfo((state) => { const allStateQuestions = state.info.questions; const currentQuestion = allStateQuestions.filter( (question) => question.id === questionId )[0]; const currentAnswer = currentQuestion.answers.find( (answer) => answer.id === parseInt(answerId) ); currentAnswer.is_chosen = !currentAnswer.is_chosen; // i even went to the extend of reassigning it yet it doesn't work currentQuestion.answers.filter((answer) => answer.id === answerId)[0] = currentAnswer; return { ...state, info: { ...state.info, questions: allStateQuestions, }, }; }); Well after using the sample logics above, none of them seem to work Thanks in advance