【问题标题】:Can't access this.state in other function无法在其他函数中访问 this.state
【发布时间】:2020-04-13 16:12:02
【问题描述】:
class Uploader extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
          data: '',
          name:'',
          loading: false
        }
    }
}

这是我在 this.state.load 中加载值的函数

 onChange(e) {
    let files = e.target.files;
    let reader = new FileReader();
    reader.readAsDataURL(files[0]);
    this.state.name = files[0].name;

    reader.onload = ((e) => {
        console.log("file is",e.target.result);
        const formData = {data: e.target.result};
        this.state.data = formData;
        this.setState({data: this.state.data});
        console.log(this.state.data); // here it gives value whats inside file
    });
    this.setState({'name': this.state.name});
    console.log(this.state.data)  // here it doesn't print anything
}

在任何函数中调用它:

onUpload() {
    console.log(this.state.data);
}

它不渲染。它给出:“错误状态未定义”。 如何在其他函数或其他代码范围中使用 this.state.data,以任何其他方式调用此值或需要此更正????

【问题讨论】:

    标签: javascript reactjs state jsx


    【解决方案1】:

    创建一个函数(箭头函数除外)会创建它自己的this 实例。 所以你的函数中没有状态对象。要解决这个问题,你有两种方法-

    使用箭头函数 -

    使用箭头函数不会创建它自己的 this 实例

     onUpload = () => {
        console.log(this.state.data) 
    }
    

    将函数的 this 绑定到类的 this

    constructor (props) {
      super(props)
      this.onChange = this.onChange.bind(this);
      this.onUpload = this.onUpload.bind(this);
    }
    

    希望这对您有所帮助。 :)

    【讨论】:

    • 绑定确实有效,根据代码标准是必要的
    【解决方案2】:

    在构造函数中使用绑定:

    constructor (props) {
      super(props)
      this.onChange = this.onChange.bind(this);
    }
    

    或者使用箭头函数:

    onChange = (e) => {
      ...
    }
    

    【讨论】:

      【解决方案3】:

      将你的方法与类绑定,否则this 将是undefined

        1.
      class Uploader extends React.Component {
      
          constructor(props) {
              super(props);
              this.state = {
                data: '',
                name :'',
                loading : false
              }
      
              this.onChange = this.onChange.bind(this);
          }
      }
      

      如果你支持的话,或者在类属性中使用箭头函数。

      1. class Uploader extends React.Component {
        
            constructor(props) {
                super(props);
                this.state = {
                  data: '',
                  name :'',
                  loading : false
                }
            }
        
            onChange = () => {}
        }
        

      【讨论】:

      • 感谢@Imran 的回答,我的代码使用绑定和'onUpload = () => {'
      • 虽然它也是正确的格式,我的代码也需要这些更改!
      【解决方案4】:

      把你的功能改成这样:

      onChange(e){
          const that = this ; // add this
          let files = e.target.files;
          let reader =new FileReader();
          reader.readAsDataURL(files[0]);
          // don't set directly value to state !
          // this.state.name = files[0].name;
          this.setState({name: files[0].name}, function() {
             console.log(that.state.name)  // here it doesn't print anything
          });
      
          reader.onload = ((e) => {
              console.log("file is",e.target.result);
              //const formData = { data : e.target.result }
              //this.state.data = formData; 
              // Use setState callback to get state result 
              that.setState({data : e.target.result}, function() {
                console.log(that.state.data) // here it gives value whats inside file
              });
      
          });
      
      
      }
      

      【讨论】:

      • 感谢 @BaBak 提供正确的编写代码标准 that.setState({data : e.target.result}, function() { console.log(that.state.data) / / 这里给出了文件 }) 中的值; 我使用了这些标准。
      猜你喜欢
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多