react 异步加载数据时的渲染问题

当数据须要异步加载时render获取不到数据可能会报一些错误,此时须要在render函数中加一个判断ios

componentWillMount (){
        axios.get("http://127.0.0.1:8088/user/userlist").then(res =>{
            this.setState({
                dataSource:res.data.data,
                haveData:true
            })

        })
    }
    render() {
     /* 注意下面这个三目运算符,执行到render时,state对象的haveData为false,
     因此此时页面展现  loading,当异步获取数据成功时,
     haveData值为true,此时又一次执行render,
     此时将须要的数据传给视图,正确展现到页面 */
        return (
            !this.state.haveData?"loading":(
                <Table columns={columns} dataSource={this.state.dataSource} />
            )

        )

    }
相关文章
相关标签/搜索