【发布时间】:2018-01-04 16:43:48
【问题描述】:
我怎样才能让我的组件在从axios GET 请求中获取数据后呈现?
我有以下组件:
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>
);
}
}
和
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>
);
}
问题在于state 的cardListBacklog 传递给CardList 没有被更新,CardList 组件中的cardList 仍然为空。我究竟做错了什么?可能是因为我使用props 设置state 属性?
【问题讨论】: