simple.tsx 1.2 KB

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