Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import { throws } from "assert";
  3.  
  4. class App extends Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. selectedPerson: null,
  9. clicked: false,
  10. selectedPlanet: null,
  11. selectedText: null
  12. };
  13. this.people = [];
  14. this.planets = [];
  15. this.texts = [];
  16. }
  17.  
  18. handleClick = () => {
  19. this.setState({
  20. clicked: true,
  21. selectedPerson: this.people[
  22. Math.floor(Math.random() * this.people.length)
  23. ],
  24. selectedPlanet: this.planets[
  25. Math.floor(Math.random() * this.planets.length)
  26. ],
  27. selectedText: this.texts[this.texts.length]
  28. });
  29. };
  30.  
  31. // getTexts = () => {
  32. // this.setState({
  33. // texts: [
  34. // "Long time ago in a galaxy far far away our hero",
  35. // "landed on the",
  36. // "planet."
  37. // ]
  38. // });
  39. // console.log(this.texts);
  40. // };
  41.  
  42. // getTexts() {
  43. // let joined = this.texts.slice();
  44. // joined.push(
  45. // "Long time ago in a galaxy far far away our hero",
  46. // "landed on the",
  47. // "planet."
  48. // );
  49.  
  50. // this.setState({ texts: joined });
  51. // console.log(joined);
  52. // }
  53.  
  54. getPlanets() {
  55. const swapi = require("swapi-node");
  56. swapi.get("https://swapi.co/api/planets/").then(result => {
  57. console.log("first page of planets", result.results);
  58. console.log(result.next);
  59.  
  60. result.results.forEach(result => {
  61. this.planets.push(result.name);
  62. // console.log(this.planets);
  63. });
  64. });
  65. }
  66.  
  67. getPeople() {
  68. const swapi = require("swapi-node");
  69. swapi.get("https://swapi.co/api/people/").then(result => {
  70. console.log("first page of people", result.results);
  71. console.log(result.next);
  72.  
  73. result.results.forEach(result => {
  74. this.people.push(result.name);
  75. // console.log(this.people);
  76. });
  77. });
  78. }
  79.  
  80. componentDidMount() {
  81. this.getPeople();
  82. this.getPlanets();
  83. }
  84.  
  85. render() {
  86. return (
  87. <React.Fragment>
  88. <button onClick={this.handleClick}>Click</button>
  89. <div>
  90. {this.state.selectedPerson} {this.state.selectedPlanet}
  91. </div>
  92. </React.Fragment>
  93. );
  94. }
  95. }
  96.  
  97. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement