I have a state in react called options that looks like:
{
"0": ["Peru", "Brazil", "Colombia", "Ecuador"],
"1": ["False", "True"],
"2": ["Kurdish", "Arab", "Egyptian", "Syrian"],
"3": ["True", "False"],
"4": ["Just Cause 2", "Grand Theft Auto 5", "The Elder Scrolls 4: Oblivion", "The Witcher 3: Wild Hunt"]
}
I want the array values to render in a radio group, In this example RadioGroup 1 should contain radio buttons of object key "0", RadioGroup 2 of object key "1" and so on...
For a more visual example:
//Radio Group 1
O Peru
O Brazil
O Columbia
O Ecuador//Radio Group 2
O False
O True
I am trying to do like this but the Radio isn't rendering in Object.values, I have tested it does render outside of Object.values if I put some constant value (for testing purpose)
const renderAnswers = (index) => { // don't worry about what index is, just remember that I'm calling this function in main render
return (
<RadioGroup aria-label="quiz" name="quiz" onChange={handleRadioChange(index)}>
{
// Works but not inside Object.values
// <FormControlLabel value="F" control={<Radio color="primary" />} label="F"} />
Object.values(options).map((val, key) => { //options is the state that I was talking about in the beginning
val.map((v, k) => {
return (
// Doesn't work I don't know why?
<FormControlLabel value={decodeEntities(v)} control={<Radio color="primary" />} label={decodeEntities(v)} />
);
})
})
}
</RadioGroup>
);
}
SideNote: because my array can contain special characters decodeEntities is a function that decodes special characters and codes and returns the string
Drew Reese solution does this:
//Radio Group 1
O Peru
O Brazil
O Columbia
O Ecuador
O False
O True
O Kurdish
O Arab
O Egyption
O Syrian
O True
O False
O Just 2 Cause
O Grand Theft Auto 5
O The Elder Scrolls 4: Oblivion
O The Witcher 3: Wild Hunt
and same for all the other radio groups
In the main render I'm doing something like
props.data.results.map((val, index) => {
return (
<Grid item xs={12}>
<Card>
<CardContent>
<Grid container spacing={6} direction="column" justify="center" alignItems="center">
<Grid item xs={12}>
<Typography
variant="h6"
component="h6">
{decodeEntities(val.question)}
</Typography>
</Grid>
<Grid item xs={12}>
{renderAnswers(index)} // notice
</Grid>
</Grid>
</CardContent>
</Card>
</Grid>
);
}
in this case data is passed as a prop through another component if you wanna more : https://github.com/JunedKhan101/Quiz-game the file you are looking for is src > Components > QuizForm.js