Guest User

Untitled

a guest
Mar 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. import MyImage from './images/myImage.jpg';
  2.  
  3. class Image extends React.Component {
  4.  
  5. constructor(props) {
  6. super(props);
  7.  
  8. this.state = {
  9. height: 0
  10. }
  11. }
  12.  
  13. getHeight = (e) => {
  14. const height = e.target.getBoundingClientRect().height;
  15.  
  16. this.setState({
  17. height: height
  18. });
  19. this.props.setUnitHeight(height);
  20. }
  21.  
  22.  
  23. render() {
  24. const image = this.props.image;
  25.  
  26. return (
  27. <img src={image.name} onLoad={(e)=>{this.getHeight(e)}} />;
  28. );
  29. }
  30. }
  31.  
  32. class App extends Component {
  33.  
  34. constructor(props) {
  35. super(props);
  36. const initUnit = 78.4;
  37.  
  38. this.state = {
  39. unit: initUnit
  40. }
  41. }
  42.  
  43. setUnitHeight = (height) => {
  44. this.setState({
  45. unit: height
  46. });
  47. }
  48.  
  49. render() {
  50. return (
  51. <div>
  52. <Image imagw={MyImage} setUnitHeight={this.setUnitHeight} />
  53. </div>
  54. );
  55. }
  56. }
Add Comment
Please, Sign In to add comment