simple.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 ImageList = styled.div`
  9. padding: 40px;
  10. display: flex;
  11. flex-wrap: wrap;
  12. align-items: center;
  13. `;
  14. const SmallImage = styled.img`
  15. margin-right: 20px;
  16. margin-bottom: 20px;
  17. width: 100px;
  18. height: 100px;
  19. cursor: pointer;
  20. `;
  21. class Example extends React.Component {
  22. state = {
  23. photoImages: ['1.png', '2.jpg', '3.jpg', '4.jpg', '5.jpg'],
  24. };
  25. render() {
  26. const { photoImages } = this.state;
  27. return (
  28. <Container>
  29. <PhotoProvider>
  30. <ImageList>
  31. {photoImages.map((item, index) => (
  32. <PhotoConsumer key={index} src={item} intro={item}>
  33. {index < 2 ? <SmallImage src={item} /> : undefined}
  34. </PhotoConsumer>
  35. ))}
  36. </ImageList>
  37. </PhotoProvider>
  38. </Container>
  39. );
  40. }
  41. }
  42. ReactDOM.render(<Example />, document.getElementById('__react-content'));