avatar.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import React from 'react'
  2. import ReactDOM from 'react-dom'
  3. import Avatar from 'react-avatar-edit'
  4. class App extends React.Component {
  5. constructor(props) {
  6. super(props)
  7. const src = './example/einshtein.jpg'
  8. this.state = {
  9. preview: null,
  10. src
  11. }
  12. this.onCrop = this.onCrop.bind(this)
  13. this.onClose = this.onClose.bind(this)
  14. this.onBeforeFileLoad = this.onBeforeFileLoad.bind(this)
  15. }
  16. onClose() {
  17. this.setState({preview: null})
  18. }
  19. onCrop(preview) {
  20. this.setState({preview})
  21. }
  22. onBeforeFileLoad(elem) {
  23. if(elem.target.files[0].size > 71680){
  24. alert("File is too big!");
  25. elem.target.value = "";
  26. };
  27. }
  28. }
  29. const Avatar = () => {
  30. return (
  31. <div>
  32. <Avatar
  33. width={390}
  34. height={295}
  35. onCrop={this.onCrop}
  36. onClose={this.onClose}
  37. onBeforeFileLoad={this.onBeforeFileLoad}
  38. src={this.state.src}
  39. />
  40. <img src={this.state.preview} alt="Preview" />
  41. </div>
  42. )
  43. }
  44. export default Avatar