Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from "react";
  2. import "./app.css";
  3.  
  4. class StarWars extends React.Component {
  5.   state = {
  6.     people: [],
  7.     nextPage: "https://swapi.co/api/people"
  8.   };
  9.  
  10.   componentDidMount() {
  11.     this.loadMore();
  12.   }
  13.  
  14.   loadMore = () => {
  15.     fetch(this.state.nextPage)
  16.       .then(r => r.json())
  17.       .then(json =>
  18.         this.setState(prevState => ({
  19.           people: [...prevState.people, ...json.results],
  20.           nextPage: json.next
  21.         }))
  22.       )
  23.       .catch(err => console.log(err));
  24.   };
  25.  
  26.   render() {
  27.     const { people } = this.state;
  28.  
  29.     return (
  30.       <div>
  31.         <h1>StarWars</h1>
  32.  
  33.         {people.map(human => (
  34.           <div className={human.gender} key={human.name}>{human.name} {human.gender}</div>
  35.         ))}
  36.  
  37.         <button onClick={this.loadMore}>load more</button>
  38.       </div>
  39.     );
  40.   }
  41. }
  42.  
  43. export default function App() {
  44.   return (
  45.     <div className="App">
  46.       <StarWars />
  47.     </div>
  48.   );
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement