Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import axios from 'axios';
  3. import { loading } from './loading';
  4.  
  5. class App extends Component {
  6. constructor(props) {
  7. super(props)
  8. this.state = {
  9. users: [],
  10. loading: false
  11. };
  12. //bind
  13. this.handleSubmit = this.handleSubmit(this);
  14. }
  15.  
  16. getUsers() {
  17. this.setState({
  18. loading: true
  19. })
  20. axios('https://api.randomuser.me/?nat=US&results=5').then(response =>
  21. this.setState({
  22. users: {...this.state.users, ...response.data.results},
  23. loading: false
  24. })
  25. );
  26. }
  27.  
  28. handleSubmit(e){
  29. e.prefentDefault();
  30. this.getUsers();
  31. console.log('more users loaded');
  32. }
  33.  
  34. componentWillMount() {
  35. this.getUsers()
  36. }
  37.  
  38.  
  39. render() {
  40. return (
  41. <div className="App">
  42. {!this.state.loading ? (
  43. this.state.users.map(user => (
  44. <div>
  45. <h2>{user.name.first}</h2>
  46. <p>{user.email}</p>
  47. <hr />
  48. <form onSubmit={this.handleSubmit}>
  49. <input type="submit" value="load users" />
  50. </form>
  51. </div>
  52. ))
  53. )
  54. : (
  55. <loading message="hey hey6 hey" />
  56. )}
  57. </div>
  58. );
  59. }
  60. }
  61.  
  62. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement