Skip to content

Instantly share code, notes, and snippets.

@MustakTalukder
Last active November 16, 2019 19:28
Show Gist options
  • Select an option

  • Save MustakTalukder/f1ba453075a76f9c8cb175fcd44c29e9 to your computer and use it in GitHub Desktop.

Select an option

Save MustakTalukder/f1ba453075a76f9c8cb175fcd44c29e9 to your computer and use it in GitHub Desktop.
import React, { useState, useEffect } from 'react';
function ContactsList(props) {
const [contactList, setcontactList] = useState([]);
const url = 'http://todorestapi.test'
const init = async () => {
const response = await fetch(url + '/api/contact/all');
const json = await response.json()
setcontactList(json)
// console.log(typeof(json.data))
// json.data.forEach(el => {
// console.log(el);
// });
console.log('init useEffect')
}
useEffect(() => {
init()
}, [])
return (
<div>
{
// JSON.stringify(contactList.data)
// contactList.data.map((contact, i) => (
// <h1>{contact.email}</h1>
// ))
console.log('return div')
}
{/* <table className="table table-dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Contact</th>
</tr>
</thead>
<tbody>
{
contactList.data.forEach(contact => (
<tr>
<th scope="row">2</th>
<td>{contact.user_name}</td>
<td>{contact.email}</td>
<td>{contact.contact_number}</td>
</tr>
))
}
</tbody>
</table> */}
</div>
);
}
export default ContactsList;
@raikusy
Copy link

raikusy commented Nov 16, 2019

import React, { useState, useEffect } from 'react';

function ContactsList(props) {
  const [contactList, setContactList] = useState(null);
  const url = 'http://todorestapi.test';

  const init = async () => {
    const response = await fetch(url + '/api/contact/all');
    const json = await response.json();
    setContactList(json.data);
  };

  useEffect(() => {
    init();
  }, []);

  return (
    <div>
      <table className="table table-dark">
        <thead>
          <tr>
            <th scope="col"> # </th>
            <th scope="col"> Name </th>
            <th scope="col"> Email </th>
            <th scope="col"> Contact </th>
          </tr>
        </thead>
        <tbody>
          {contactList &&
            contactList.map(contact => (
              <tr>
                <th scope="row"> 2 </th>
                <td> {contact.user_name}</td>
                <td> {contact.email}</td>
                <td> {contact.contact_number}</td>
              </tr>
            ))}
        </tbody>
      </table>
    </div>
  );
}

export default ContactsList;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment