Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import './App.css';
  3. // import CelebList from './components/CelebList';
  4. import { Button } from 'reactstrap';
  5. import Contact from "./contacts.json"
  6. import { Table } from "reactstrap";
  7.  
  8.  
  9.  
  10. const CelebItem = ({ blah }) => {
  11. return (
  12. <tr>
  13. <td><img src={blah.pictureUrl} width="100" height="150" alt="" /> </td>
  14. <td>{blah.name}</td>
  15. <td>{blah.popularity}</td>
  16. {/* <td><Button color="danger" onClick={deleteItem}> Delete</Button></td> */}
  17. </tr >
  18. )
  19. }
  20.  
  21. const CelebList = ({ contact }) => {
  22. return (
  23. <Table hover>
  24. <thead>
  25. <tr>
  26. <th>Picture:</th>
  27. <th>Name:</th>
  28. <th>Popularity:</th>
  29. </tr>
  30. </thead>
  31. <tbody>
  32. {contact.map((n, i) => (
  33. <CelebItem key={i} blah={n} />
  34. ))}
  35. </tbody>
  36. </Table>
  37. );
  38. };
  39.  
  40. class App extends Component {
  41.  
  42. state = {
  43. contact: Contact.slice(0, 5)
  44. }
  45.  
  46.  
  47. addRandomContact = () => {
  48. const randomIdx = Math.floor(Math.random() * Contact.length)
  49. this.state.contact.push(Contact[randomIdx])
  50. this.setState({ contact: this.state.contact })
  51. }
  52.  
  53. sortByName = (i) => {
  54. this.setState({
  55. contact: this.state.contact.sort((a, b) => a.name > b.name ? 1 : -1)
  56. })
  57. }
  58.  
  59. sortByPopularity = (i) => {
  60. this.setState({
  61. contact: this.state.popularity.sort((a, b) => a.popularity > b.popularity ? 1 : -1)
  62. })
  63. }
  64.  
  65.  
  66.  
  67.  
  68. render() {
  69. // console.log(this.state.contact)
  70. return (
  71. <div>
  72. <div>
  73. <Button color="primary" onClick={this.addRandomContact}>Add Random Contact</Button>
  74. <Button color="primary" onClick={this.sortByName}>Sort</Button>
  75. <Button color="primary" onClick={this.sortByName}>Sort by Popularity</Button>
  76.  
  77.  
  78.  
  79.  
  80. </div>
  81. <div>
  82. <CelebList contact={this.state.contact}
  83. />
  84.  
  85.  
  86. </div>
  87. </div>
  88.  
  89. );
  90. }
  91. }
  92.  
  93. export default App
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement