I am having trouble getting this working with my React state. The set up below works and I can access the data I need, however I am looking to expand the 'questions' field so that it is an array of objects opposed to a single object.
this.state = {
ballots: [
{
id: '2222' ,
title: 'Test Ballot',
questions: {title: 'Question 1', option: 'option 1' }
}
]
}
looking to have the state be like this.
this.state = {
ballots: [
{
id: '2222' ,
title: 'Test Ballot',
questions: [{title: 'Question 1', options: ['option 1', 'option 2', 'option 3'] }, {title: 'Question 2', option: ['option 1', 'option 2', 'option 3']}]
}
]
}
I am mapping these value to another component as shown below.
return (
<div className='ballot-page'>
{this.state.ballots.map(
({id, ...props}) => (
<BallotForm key={id} {...props} />
)
)}
</div>
);
Would I need to map all the 'ballots' and then map again over each of the 'questions' to get all of the fields within it? Let me know if I can clarify this any better. Below is the BallotForm component.
const BallotForm = ({ title, questions }) => (
<div className='ballot-form-page'>
<div className='ballot-form'>
<div className='ballot-title'>{title}</div>
<span>{questions.title}</span>
<form>
<span>{questions.option}</span>
<input type='checkbox'></input>
</form>
</div>
</div>
);