代码之家  ›  专栏  ›  技术社区  ›  Nikul Panchal

this.state不在react js中的reader.onload函数上工作

  •  0
  • Nikul Panchal  · 技术社区  · 6 年前

    我想设定状态,但是 this.state 不起作用了,谁能检查一下这个代码,帮我看看有什么问题,这是我的代码

    save_import_permit() {
          //alert('sdsdsd');
          var file_name =  jQuery('input[type=file]').val().split('\\').pop(); //jQuery('#permit_csv_file').prop('files')[0];
          let files = jQuery('input[type=file]')[0].files[0];
          console.log(files);      
          let reader = new FileReader();  
          let data = [];
          reader.readAsDataURL(files);
          reader.onload = function(e) {    
            console.log(e.target.result);   
            data.result = e.target.result;
            data.action = 'Permity::saveImportCsvFile';  
            const url  = 'admin-ajax.php';
            const formdata = "file="+data.result+"&action="+data.action;       
            this.state({loading:'loading_show'})    
            return post(url,formdata,function (r) {
              this.state({loading:'loading_hide'});        
              alert(r.data.msg)          
            }.bind(this));   
          }.bind(this);     
    
      }  
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Vencovsky    6 年前

    您不应该使用 this.state ,您应该始终使用 this.setState() ,wichs是react框架的内置函数。检查这个 link 为了更好地了解。

    所以你需要改变

    this.state({loading:'loading_show'})

    this.setState({loading:'loading_show'})