simple.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import styled from 'styled-components';
  4. import { PhotoProvider, PhotoConsumer } from '../src';
  5. const Container = styled.div`
  6. font-size: 32px;
  7. `;
  8. const Header = styled.header`
  9. padding: 40px;
  10. font-size: 32px;
  11. border-bottom: 1px solid #ccc;
  12. `;
  13. const ImageList = styled.div`
  14. padding: 40px;
  15. display: flex;
  16. flex-wrap: wrap;
  17. align-items: center;
  18. `;
  19. const SmallImage = styled.img`
  20. margin-right: 20px;
  21. margin-bottom: 20px;
  22. width: 100px;
  23. height: 100px;
  24. cursor: pointer;
  25. `;
  26. class Example extends React.Component {
  27. state = {
  28. photoImages: ['1.png', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg'],
  29. };
  30. render() {
  31. const { photoImages } = this.state;
  32. return (
  33. <Container>
  34. <Header>React 图片预览组件</Header>
  35. <PhotoProvider>
  36. <ImageList>
  37. {photoImages.map((item, index) => (
  38. <PhotoConsumer key={index} src={item}>
  39. {index < 2 ? <SmallImage /> : undefined}
  40. </PhotoConsumer>
  41. ))}
  42. </ImageList>
  43. </PhotoProvider>
  44. </Container>
  45. );
  46. }
  47. }
  48. ReactDOM.render(<Example />, document.getElementById('root'));