React quiz app

I'm currently working on a quiz app for a Scrimba project but I'm a bit stuck on how to proceed. Right now, my app can fetch data from the API and display them on the page. However, I'm not sure how to make only one button active per question or how I should manage state for user selections (would this go in <App /> or in the component that's rendering the quiz questions?). Hopefully this made sense and I would appreciate it if anyone could take a look and offer some suggestions. https://codesandbox.io/s/quizzical-nqjq39
savvystrider
CodeSandbox
quizzical - CodeSandbox
quizzical by savvystrider using html-entities, loader-utils, react, react-dom, react-scripts, uuid
7 Replies
Zoë
Zoë8mo ago
Hold the selection in a state and then make whichever option selected highlighted. Also for things like this use radio buttons because you want only 1 thing selected
savvystrider15
savvystrider158mo ago
I tried the radio button approach but it got a little tricky for me managing name, value, and checked attributes for those elements. But it does seem to be preferable to using regular buttons for this
Zoë
Zoë8mo ago
It is worthwhile going the radio button route
savvystrider15
savvystrider158mo ago
Refactored my code a bit to use a form/radio buttons but I'm running into trouble with checkAnswers function. Would appreciate it if anyone has the time to take a look and offer any suggestions: https://codesandbox.io/s/quizzical-nqjq39
savvystrider
CodeSandbox
quizzical - CodeSandbox
quizzical by savvystrider using html-entities, loader-utils, react, react-dom, react-scripts, uuid
Zoë
Zoë8mo ago
I logged what was being compared with console.log({ answers, selectedChoice, correctChoice }) and you should have stored the index in answers
No description
savvystrider15
savvystrider158mo ago
Does that mean answers needs to be converted from an array into an object in order to hold the index?
Zoë
Zoë8mo ago
No, an array of indices
Want results from more Discord servers?
Add your server
More Posts