Hi
I would like to display the data after an API call but presumably the async call finishes after the render() so I am lost.
import React, { Component } from 'react';
import styles from '../layout/styles';
import {Doughnut} from 'react-chartjs-2';
import Api from '../../utils/ApiManager';
import Chart from 'chart.js';
import {Link} from 'react-router';
const data = {
labels: [
'Red',
'Green',
'Yellow'
],
datasets: [{
data: [300, 50, 100],
backgroundColor: [
'#FF6384',
'#36A2EB',
'#FFCE56'
],
hoverBackgroundColor: [
'#FF6384',
'#36A2EB',
'#FFCE56'
]
}]
,json :{ "id": "test"}
};
class Polldetail extends Component {
constructor() {
super()
this.state = {
selected: 0,
list: [],
data:data
}
}
componentWillMount(){ // was using componentDidMount()
console.log('componentDidMount (Polldetail): ' + this.props.location.pathname);
//console.dir(this.props);
var urlWithId =this.props.location.pathname;
var pollID = urlWithId.split('/').pop();
Api.get('/api/polls/' + pollID, null, (err, response) => {
if (err) {
alert("Error: " + err);
return;
}
console.log('RESULTS: ' + JSON.stringify(response.message));
this.setState({
list: response.message
});
data.json = response.message;
});
}
vote(){}
eachPollResponse(resp) {
return (<div className="responses">
<p>test</p>
<input ref="newText" type="text" className="form-control"></input>
<button onClick={this.vote}>Vote</button>
</div>)
}
render() {
//const zoneStyle = styles.zone; // needs to be inside the render func!
//cool
return(<div className="container">
<div className="row">
<div className="col-md-6">
<Link to="/">Back</Link>
<h2>{this.state.list.pollquestion}</h2>
{this.state.list.responses.map(this.eachPollResponse)}
</div>
<div className="col-md-6">
<Doughnut data={this.state.data} />
</div>
</div>
</div>);
}
}
export default Polldetail
Console output shows the API call finishes after the render
The line that fails is in the render:
{this.state.list.responses.map(this.eachPollResponse)}