【问题标题】:this.setState is not a function ( trying Upload Image) ReactJsthis.setState 不是一个函数(尝试上传图片)ReactJs
【发布时间】:2017-07-17 16:12:25
【问题描述】:

尝试使用 cropper 组件创建上传功能。但是当我尝试设置状态时,它会说

未捕获的类型错误:this.setState 不是 FileReader.reader.onload 中的函数

组件

import React, { Component, PropTypes } from 'react'
import { Link, browserHistory } from 'react-router'
import { connect } from 'react-redux'
import Cropper from 'react-cropper';

class HeadPortrait extends Component {
  constructor(props) {
    super(props)
    this.state = {
      image: '',
    }
  }

  clickadd(e) {
    document.getElementById('uploadfiles').click();
  }

  loadFile(e) {
    e.preventDefault();
    var reader = new FileReader();
    reader.onload = function () {
      this.setState({ image: reader.result })
    };
    reader.readAsDataURL(e.target.files[0]);
    ;
  };

  _crop() {
    // image in dataUrl
    console.log(this.refs.cropper.getCroppedCanvas().toDataURL());
  }

  render() {
    return (
      <div className="group-content width415">
        <input id="uploadfiles" type="file" accept="image/*" onChange={this.loadFile.bind(this)}></input>
        <a onClick={this.clickadd.bind(this)} className="gt-btn-small gt-left ">Add Image</a>
        <div>
          <div className="bigImageprev gt-left"><img width="300" height="300" id="output" /></div>
        </div>
        <div className="gt-clear"></div>
        <div className="btnSaveImage gt-right"><a className="gt-btn-small" href="#">Save</a></div>

        <Cropper
          ref='cropper'
          src={this.state.image}
          style={{ height: 400, width: '100%' }}
          // Cropper.js options
          aspectRatio={16 / 9}
          guides={false}
          crop={this._crop.bind(this)} />
      </div>
    )
  }
}

function mapStateToProps(state) {
  return {
    // stories: state.stories
  }
}

export default connect(mapStateToProps)(HeadPortrait)

尝试像示例中的https://github.com/roadmanfong/react-cropper/blob/master/example/src/Demo.jsx

【问题讨论】:

标签: javascript reactjs image-upload


【解决方案1】:

问题是您用于onloadfunctionthis 没有您的课程this。尝试任一箭头功能:

reader.onload = () => this.setState({ image: reader.result })

或者之前将this赋值给一个变量

const scope = this
reader.onload = function () {
  scope.setState({ image: reader.result })
}

此外,值得在构造函数中绑定事件处理程序而不是直接在渲染中,因为这样无论状态如何变化,您都将重新渲染组件 - 因为事件处理程序每​​次都是一个新引用:

constructor(props) {
  super(props)
  this.state = {
    image: '',
  } 
  this.loadFile = this.loadFile.bind(this) // properly bound once
}

在渲染中:

<input onChange={this.loadFile}></input>

【讨论】:

    【解决方案2】:

    reader.onload 函数需要绑定到this。然后它将在范围内有this.setState

    var onloader = function(){this.setState({ image: reader.result }); reader.onload = onloader.bind(this);

    你也可以只使用箭头函数:

    reader.onload = ()=>{this.setState({ image: reader.result });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-08
      • 2015-05-01
      • 2020-12-11
      • 2018-12-11
      • 2018-09-22
      • 2015-09-11
      • 1970-01-01
      相关资源
      最近更新 更多