Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. import React, {Component} from 'react'
  2.  
  3. class App extends Component {
  4.  
  5. constructor(props){
  6. super(props)
  7. this.SelectUser= React.createRef()
  8. this.onHandleUsers = this.onHandleUsers.bind(this)
  9. this.onHandleCurrentUsers = this.onHandleCurrentUsers.bind(this)
  10. this.state = {
  11. isLoaded: false,
  12. users: []
  13. }
  14. }
  15. onHandleCurrentUsers() {
  16. const userId=this.SelectUser.current.value
  17. const data = {userId: userId}
  18.  
  19. fetch("http://localhost:3000/api/user", {
  20. method: 'POST',
  21. headers: { "Content-Type": "application/json"},
  22. body: JSON.stringify(data)
  23. })
  24. .then(res => res.json())
  25. .then(result => {
  26. this.setState({
  27. isLoaded: false,
  28. currentUser: result
  29. })
  30. })
  31. .catch(e => console.log(e))
  32. }
  33. onHandleUsers() {
  34. fetch("http://localhost:3000/api/users")
  35. .then(res => res.json())
  36. .then(result => {
  37. this.setState({
  38. isLoaded: true,
  39. users: result
  40. })
  41. })
  42. .catch(e => console.log(e))
  43. }
  44.  
  45. render() {
  46. const {currentUser} = this.state
  47. return (
  48. <div>
  49. <button onClick={this.onHandleUsers}>Gauti vartotojus</button>
  50. <ul>
  51. {
  52. this.state.isLoaded && this.state.users.map(item => {
  53. return(
  54. <li>Vardas: {item.name}, pavarde: {item.surname}</li>
  55. )
  56. })
  57. }
  58. </ul>
  59. <div>
  60.  
  61. <input type="number" ref={this.SelectUser} />
  62.  
  63. <button onClick ={this.onHandleCurrentUsers}>Gauti pasirinkta vartotoją</button>
  64. <div>
  65. {
  66. currentUser && <div>{currentUser.name} {currentUser.surname}</div>
  67. }
  68. </div>
  69.  
  70. </div>
  71. </div>
  72. )
  73. }
  74. }
  75.  
  76.  
  77. export default App
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement