How can I make it so that my components render after getting the data from an axios GET request?
I have the following components:
class CardPool extends React.Component{
constructor(props){
super(props);
this.state = {
cardListBacklog: [],
};
}
componentWillMount(){
axios.get('/cards').then(function(response){
this.setState({cardListBacklog: response.data});
}.bind(this));
}
render(){
return(
<div class="row">
<Section name="Construction of Component 1 with a huge ass name that wont really fit in the section">
<Column columnName="BACKLOG">
<CardList id="card-list-1" column="0" cardList={this.state.cardListBacklog}/>
</Column>
</Section>
</div>
);
}
}
and
class CardList extends React.Component{
constructor(props){
super(props);
this.state = {
cardList: [],
laneID: this.props.column
}
}
componentWillMount(){
this.setState({
cardList: this.props.cardList,
});
}
render(){
const {connectDropTarget} = this.props;
return connectDropTarget(
<div class={"card-list " + this.addClass()} id={this.props.id}>
{
this.state.cardList ?
this.state.cardList.map((card) => {
return <Card key={card.id} state={card} laneID={this.state.laneID}/>
})
: null
}
</div>
);
}
The problem is that the state's cardListBacklog passed to the CardList is not being updated, and the cardList in CardList component remains empty. What am I doing wrong? Could it be because I am setting state properties using the props?