Chciałbym usuwać swoje zagadki po udzieleniu odpowiedzi przez zalogoweanego użytkownika, problem jest z przekazaniem id który jest undefined przez co nie moge usunac tej zagadki bo mam komunikat z konsoli:
Error: DELETE http: // api / Remove / undefined 400 (Bad Request)
Oto fragment mojego kodu :
state={
qBank:[{id:1,question:'dd',answers:['a','b','c','d'],correct:'a'}]
}
render:
this.state.qBank.map(
({ question, answers, correct, id }) => (
<QuestionBox key={id} id={id} question={question} options={answers} selected={answer => this.computeAnswer(answer, correct)} />
)
)}
questionBox:
const QuestionBox = ({ question, options, selected, key, id }) => {
const [answer, setAnswer] = useState(options);
const [alreadyAnswered, setAlreadyAnswered] = useState(false);
return (
<div className="questionBox">
<div className="question">{question}</div>
{(answer || []).map((text, index) => (
<button disabled={alreadyAnswered} key={index} className="answerBtn" onClick={(id) => {
if (!alreadyAnswered) {
setAnswer([text]);
selected(text);
setAlreadyAnswered(true);
fetch("http:/api/Remove/" + id, {
method: 'DELETE',
headers: {
"Content-Type": "application/json",
"Authorization": `bearer ${sessionStorage.getItem("access_token")}`
},
}).then(() => {
}).catch(err => {
console.error(err)
})
}
}}>{text}</button>
))}
</div>
)
}