Advertisement
Guest User

getById

a guest
Jan 18th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {Component} from "react";
  2. import c from './Account.module.css'
  3. import ApiService from "../../../ApiService";
  4.  
  5. class Account extends Component {
  6.     accountItem = {
  7.         id: '',
  8.         name: '',
  9.         email: '',
  10.         password: '',
  11.         link: ''
  12.     };
  13.  
  14.     constructor(props) {
  15.         super(props);
  16.         this.state = {
  17.             item: this.accountItem
  18.         };
  19.         this.reloadAccount = this.reloadAccount.bind(this);
  20.     }
  21.  
  22.     componentDidMount() {
  23.         this.reloadAccount(this.id);
  24.     }
  25.  
  26.     reloadAccount(id) {
  27.         ApiService.fetchAccountById(id)
  28.             .then((res) => {
  29.                 let account = res.data;
  30.                 this.setState({
  31.                     id: account.id,
  32.                     name: account.name,
  33.                     email: account.email,
  34.                     password: account.password,
  35.                     link: account.link
  36.                 })
  37.             })
  38.     }
  39.  
  40.     render() {
  41.         const {item} = this.state;
  42.  
  43.         return (
  44.             <div>
  45.                 <div className={c.account}>
  46.                     <ul>
  47.                         <li>Id: {item.id}</li>
  48.                         <li>Name: {item.name}</li>
  49.                         <li>Email: {item.email}</li>
  50.                         <li>Password: {item.password}</li>
  51.                         <li>Link: {item.link}</li>
  52.                     </ul>
  53.                 </div>
  54.             </div>
  55.         )
  56.     }
  57. }
  58.  
  59. export default Account;
  60.  
  61. ApiService:
  62. const ACCOUNT_API_BASE_URL = 'http://localhost:8080/api/accounts';
  63. ...
  64. fetchAccountById(id) {
  65.         return axios.get(ACCOUNT_API_BASE_URL + '/' + id);
  66.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement