I have an form with radio inputs. Each time the user selects on option, I must update the array containing the answers to the questions overriding old answers if it is the same question the user answered. So I have this useState
that, in my thought, would map the old values of answers, map if the answer to the question is already there and Object.assign() it, otherwise if it is a new answer to a new question, then just return the answer. But it only returns the default array. If it is initialized like [{}] it returns Array[{}]. If it is [], returns Array[]
State is
const [answers, setAnswers] = setState([])
const handleCheckbox = (e, question) => {
const newAnswer = { choice: e.target.id, question: question.uuid };
setAnswers(
[...answers],
answers.map(
answer =>
answer.question === newAnswer.question ?
Object.assign(answer, newAnswer):
newAnswer
)
);
};
Expected: answers: [{id: 1}, {id: 2},...] without repeating those already there.
Actual: Array[]