Try create upload function with cropper component. But when I try set state it says that
Uncaught TypeError: this.setState is not a function at FileReader.reader.onload
Component
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)
Trying do like in example https://github.com/roadmanfong/react-cropper/blob/master/example/src/Demo.jsx