Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. class App extends React.Component{
  2. constructor(props)
  3. {
  4. super(props);
  5. this.state={
  6. contacts: [
  7. {
  8. name: 'Jan',
  9. tel: '123 456 789'
  10. },
  11. {
  12. name: 'Krzyś',
  13. tel: '098 765 432'
  14. }
  15. ]
  16. }
  17. }
  18.  
  19. addContact(contact)
  20. {
  21. console.log("!!" + contact.name+" "+contact.tel);
  22. const tempContacts = this.state.contacts.concat([
  23. {
  24. name: contact.name, //Czyli tu powinien rozrzerzyć tablicę o dodatkowy obiekt z nowymi wartościami
  25. tel: contact.tel
  26. }
  27. ])
  28.  
  29. this.setState(
  30. {
  31. contacts: tempContacts, //I tu zastąpić starą tablicę, nową bogatszą o element
  32. },
  33. ()=>{ console.log(this.state.contacts)}
  34. )
  35.  
  36. }
  37.  
  38. render()
  39. {
  40. return(
  41. <div>
  42. <Form addContact={(contact) => this.addContact(contact)}/>
  43. <div>
  44. <Book contacts={this.state.contacts}/>
  45. </div>
  46. </div>
  47. )
  48. }
  49. }
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. class Book extends React.Component{
  59. render()
  60. {
  61. const contacts = this.props.contacts.map(
  62. (contact,id)=>{
  63. return(
  64. <li key={id}> | {contact.name} : {contact.tel}</li>
  65. )
  66. });
  67.  
  68. return(<ol>{contacts}</ol>)
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement