【问题标题】:How to innerHTML a jsx element on react如何在反应中对一个jsx元素进行innerHTML
【发布时间】:2020-03-23 17:08:02
【问题描述】:

我一直在尝试制作一个登录表单,当你点击注册时,注册按钮会变成一个加载图标,如果它从服务器收到false 响应,它会再次加载按钮而不是加载微调器,但我遇到了一个问题,当它应该删除加载微调器并再次显示注册按钮(使用innerHTML)时,它只显示 [object Object] 而不是按钮,所以这是我用来渲染按钮的语句, 微调器:

     onSubmit = () => {
            document.getElementById('error-alert').innerHTML = "";
            document.getElementById('sumbit-btn').innerHTML = ` //rendering the loading spinner
            <span class="btn btn-light border-1 border-dark mdi mdi-loading mdi-spin mdi-24px"></span>`;
            fetch("my server link here", {
              method: 'post',
              headers: { 'Content-Type': 'application/json' },
              body: JSON.stringify({
                email: this.state.email,
                password: this.state.password,
                name: this.state.name
              })
            })
              .then(response => response.json())
              .then(user => {
                if (user.id) {   // if the input is correct (response true from the server)
                  this.props.loadUser(user)
                  this.props.onRouteChange('home');
}



 else {  //if the response is false from the server


    ///////////////   this is the innerHTML i mean:   //////////
                  document.getElementById('sumbit-btn').innerHTML =
                    <div
                      className="btn btn-light border-1 border-dark"
                      onClick={this.onSubmit}
                    >Register
                  </div>
    ///////////////////////////////////////////////////////////

                }
              })
          }

注意这个代码块在render(){}之外

【问题讨论】:

    标签: javascript reactjs jsx innerhtml


    【解决方案1】:

    我认为最简单的方法是在状态中存储一个布尔值,然后在渲染中创建一个三元组。根据您的需要更改布尔值。

     onSubmit = () => {
            document.getElementById('error-alert').innerHTML = "";
            this.setState({showLoading: true});
            <span class="btn btn-light border-1 border-dark mdi mdi-loading mdi-spin mdi-24px"></span>`;
            fetch("my server link here", {
              method: 'post',
              headers: { 'Content-Type': 'application/json' },
              body: JSON.stringify({
                email: this.state.email,
                password: this.state.password,
                name: this.state.name
              })
            })
              .then(response => response.json())
              .then(user => {
                if (user.id) {   // if the input is correct (response true from the server)
                  this.props.loadUser(user)
                  this.props.onRouteChange('home');
     } else {
       this.setState({showLoading: false});
    }
    render(){
       return(
         <div>{show ? 
                        <Loading/> 
                        :
                        <div
                          className="btn btn-light border-1 border-dark"
                          onClick={this.onSubmit}
                        >Register
                      </div>
              }
      )
    }
    

    【讨论】:

    • 或者,如果您只是想向按钮添加一个类名,您也可以这样做,但只需在类名中使用三元组。
    • 是的,我实际上是通过添加一个新的状态元素并为它自己的按钮创建一个新组件并在提交组件中执行 if 语句来解决的,类方法看起来也不错,哈哈,谢谢
    猜你喜欢
    • 2018-01-01
    • 2019-03-20
    • 2018-11-15
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    相关资源
    最近更新 更多