直接上图吧
先是请求了接口里的数据
再通过 setState 改了数据
但是map 不执行 ,但我切换页面再返回的时候代码却神奇的遍历出来了
怎么才能让他进入页面就渲染出列表呢 ? 跪求大神解答 !
各位大神帮我看看这个代码 ,小白求助
Addre
#1
oyb81076
#2
不要往state里放promise对象, 数据获取到之后用 setState, 才会触发渲染
class HomePage extends Component {
state = {
arr: [],
isLoading: false
}
componentWillMount(){
this.load()
}
async load(){
this.setState({isLoading: true});
const houses = await Apis.instance().db_api().exec("get_hourses",[["1.14.1"]]);
const arr = await Promise.all(house[0].rooms.map(async (v)=>{
return Apis.instance().db_api().exec("get_seer_room", [v, 0, 0])
}));
this.setState({isLoading: false, arr })
},
// .....
}
1 Like