Guest User

Untitled

a guest
Aug 3rd, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. // React 16.4.1
  2.  
  3. import React, { Component } from "react";
  4.  
  5. class Contacts extends Component {
  6. // Our State, with objects
  7. state = {
  8. // arrays of objects
  9. contacts: [
  10. {
  11. id: 1,
  12. name: "John Doe",
  13. email: "jdoe@gmail.com"
  14. },
  15. {
  16. id: 2,
  17. name: "Karen Williams",
  18. email: "karen@gmail.com"
  19. },
  20. {
  21. id: 3,
  22. name: "Henry Johnson",
  23. email: "henry@gmail.com"
  24. }
  25. ]
  26. };
  27.  
  28. deleteContact = id => {
  29. // extract 'contacts' from 'state'
  30. const { contacts } = this.state;
  31.  
  32. // create a new copy of contacts using filter to remove desired id
  33. const newContacts = contacts.filter(contact => contact.id !== id);
  34.  
  35. // Use setState() to set the new copy 'contacts' object with the removed id omitted
  36. this.setState({
  37. contacts: newContacts
  38. })
  39. };
  40.  
  41. render() {
  42. // extract 'contacts' from 'state
  43. const { contacts } = this.state;
  44.  
  45. // Remove this component from view via event handler
  46. <Contact key={contact.id} contact={contact} deleteClickHandler={this.deleteContact.bind(this.contactid)} />
  47. }
  48.  
  49. }
Add Comment
Please, Sign In to add comment