【问题标题】:Introduce a warning if no result of getstudents api call如果 getstudents api 调用没有结果,则引入警告
【发布时间】:2019-07-07 12:57:48
【问题描述】:

我刚开始使用 reactjs 和 api 的 .. 尝试在 react 应用中添加任何新主题时,您需要将其与教师相关联。但是,如果您没有指定教师(第一次使用),则教师插槽将被禁用。

如果 getteacher 调用没有结果,我如何在模态顶部引入警告。(说“你没有分配老师..”

【问题讨论】:

    标签: javascript json reactjs api callback


    【解决方案1】:

    为此,您需要state

    constructor(props){
        super(props);
        this.state = {
           isTeacherAssigned:false //Initially false
        }
    }
    

    然后当你调用你的 API 时,你得到空响应来设置状态,

    fetch(url)
    .then(response =>{
        return response.json();
    })
    .then(response =>{
        //check if response is empty
        if(responseEmpty){
           this.setState({isTeacherAssigned:false})
        }else{
           this.setState({isTeacherAssigned:true})
        }
    })
    

    在渲染中,

    {!this.state.isTeacherAssigned && <div>You dont have a teacher assigned..</div>}
    

    【讨论】:

      猜你喜欢
      • 2013-02-19
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-29
      相关资源
      最近更新 更多