So I already posted about my react quiz app I am working on, the thing is I ran into another snag. I am using an api call in a react app to make a randomized quiz application. The problem I have is how to add the answers to the questions. I have already put up the questions no problem, but within the question’s mapping function I expect I need to use another map function to apply the answers. I have tried using another .then() function but it appears that I lose the data of the api call at that point. The app currently doesn’t display the questions until I remove the added map function, I kept it on there to express what I had tried so far. I’m sure it should be a pretty simple fix, I am just quite the newbie at all of this. Thanks for your help.
here is the codepen
Here is the jsx if you just want to see that
var qs;
var answers ;
let thing;
class Quiz extends React.Component{
constructor(props){
super(props);
}
render(){
return (
<div className="box">
<h1 className=" display-3 borderbottom">Quiz app</h1>
<QuizContent getAs={this.getAs}/>
</div>
);
}
}
class QuizContent extends React.Component {
constructor(props){
super(props);
this.state = {
questions: [],
answers: [],
possible_answers: []
};
}
componentDidMount(){
fetch('https://opentdb.com/api.php?amount=10&category=12').then(results => {return results.json();})
.then(data =>{
let qs = data.results.map((item, i)=>{
const entities = {
''': "'",
'"': '"',
"ñ": "ñ",
"é": "é",
"&": "&" ,
"ü": "ü"
}
return (
<div>
<div key= {data.results[i]}>{data.results[i].question.replace(/&#?\w+;/g, match => entities[match])}
<br />
<br />
<div> {data.results[i].incorrect_answers.map((item,j)=> {
return (
<p>{data.results[j].incorrect_answers; } </p>
)
})} </div>
<br />
</div>
</div>
)
})
this.setState({
questions: qs,
})
})
}
render(){
return(
<h2>{this.state.questions}</h2>
)
}
}
ReactDOM.render(<Quiz />, document.getElementById("app")
);
I just have the code to map the incorrect answers, I was going to code the correct answers afterwards and stylize the inputs. Thanks again !