Advertisement
Guest User

bbz

a guest
Nov 14th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. import React, {Component} from 'react'
  2.  
  3.  
  4.  
  5.  
  6. class App extends Component {
  7.  
  8. constructor(props){
  9. super(props)
  10. this.onHandleUsers = this.onHandleUsers.bind(this)
  11.  
  12.  
  13. this.state = {
  14. isLoaded: false,
  15. users: []
  16. }
  17. }
  18.  
  19.  
  20.  
  21.  
  22. handleUserChange = (e) => {
  23. const {value} = e.target
  24. this.onHandleCurrentUsers(value)
  25.  
  26. }
  27.  
  28. onHandleUsers() {
  29. fetch("http://localhost:3000/api/users")
  30. .then(res => res.json())
  31. .then(result => {
  32. this.setState({
  33. isLoaded: true,
  34. users: result
  35. })
  36. })
  37. .catch(e => console.log(e))
  38. }
  39.  
  40. onHandleCurrentUsers(userId) {
  41. const data = {userId: userId}
  42. fetch("http://localhost:3000/api/user", {
  43. method: 'POST',
  44. headers: { "Content-Type": "application/json"},
  45. body: JSON.stringify(data)
  46. })
  47. .then(res => res.json())
  48. .then(result => {
  49. this.setState({
  50. isLoaded: false,
  51. currentUser: result
  52. })
  53. })
  54. .catch(e => console.log(e))
  55. }
  56.  
  57.  
  58.  
  59.  
  60. render() {
  61. const {currentUser} = this.state
  62. return (
  63. <div>
  64.  
  65.  
  66. <button onClick={this.onHandleUsers}>Gauti vartotojus</button>
  67. <ul>
  68. {
  69. this.state.isLoaded && this.state.users.map(item => {
  70. return (
  71. <ol>Vardas: {item.name} <br>
  72. </br>Pavardė: {item.surname}</ol>
  73. )
  74. })
  75. }
  76.  
  77. </ul>
  78. <button onClick ={e => this.onHandleCurrentUsers(1)}>Gauti pirma vartotoja</button>
  79. <button onClick ={e => this.onHandleCurrentUsers(2)}>Gauti antra vartotoja</button>
  80. <div>
  81. {
  82. currentUser && <div>{currentUser.name} {currentUser.surname}</div>
  83. }
  84. </div>
  85.  
  86.  
  87. <br></br>
  88. <br></br>
  89. <br></br>
  90. <br></br>
  91. <br></br>
  92. <br></br>
  93.  
  94. {/* antra dalis */}
  95. <div>
  96.  
  97. <input name='userId' onChange={this.handleUserChange} />
  98.  
  99.  
  100.  
  101. <p></p>
  102. <p></p>
  103. <p></p>
  104. </div>
  105.  
  106. <div> </div>
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113. </div>
  114.  
  115. )
  116. }
  117. }
  118.  
  119.  
  120. export default App
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement