repl.it
@abouthalf/

RemoteDataWithReact

React

No description

fork
loading
Files
  • src
    • App.js
    • App.css
    • index.css
    • index.js
    • logo.svg
  • Packager files
  • package.json
src/App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import React, { Component } from 'react';
import './App.css';

class App extends Component {

  state = {
    photos: []
  }

  componentDidMount() {
    fetch("https://jsonplaceholder.typicode.com/photos")
      .then(res => res.json())
      .then(photos => {
        photos = photos.slice(0,9);
        this.setState({ photos });
      })
      .catch(err => {
        this.setState({ photos: [] });
      })
  }

  renderPhoto({ title, thumbnailUrl, url }) {
    return <li>
      <a href={url}>
        <img src={thumbnailUrl} alt={title} />
      </a>
    </li>
  }

  render() {
    const { photos } = this.state;
    return (
      <div className="App">
        <ul>
          {photos.map(p => this.renderPhoto(p))}
        </ul>
      </div>
    );
  }
}

export default App;