-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3d4745
commit 25a6489
Showing
7 changed files
with
93 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useState } from 'react'; | ||
import useData from './useData'; | ||
|
||
|
||
export function App() { | ||
const {loading, error, products} = useData() | ||
|
||
const columns = products.length > 0 ? Object.keys(products[0]).filter((key) => key !== 'image') : []; | ||
|
||
|
||
return ( | ||
<> | ||
<div className=" flex items-center p-6 text-xl "> | ||
{products && | ||
<table className='min-w-full bg-white shadow-lg rounded-lg'> | ||
<thead className='bg-gray-200 px-6 py-3 text-left text-xl font-bold capitalize'> | ||
{columns.map((column)=><th className='px-4 py-2'>{column}</th>)} | ||
</thead> | ||
<tbody> | ||
{products.map((product)=>( | ||
<tr key={product.name} className='hover:bg-gray-100 transition-colors'> | ||
{columns.map((column)=>(<td className='px-4 py-2 border border-gray-200 capitalize '>{product[column]}</td>))} | ||
</tr> | ||
)) | ||
} | ||
</tbody> | ||
</table> | ||
} | ||
</div> | ||
<div> | ||
{ loading == true && <div className='flex items-center justify-center text-xl '>Loading....</div> } | ||
{error && <p>Error Fetching Data</p>} | ||
</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useEffect } from 'react'; | ||
import { useState } from 'react'; | ||
|
||
function useData() { | ||
const url = 'https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json'; | ||
const [products, setProducts] = useState([]); | ||
const [loading, setLoading] = useState(true); | ||
const [error, setError] = useState(null); | ||
useEffect(()=>{ | ||
const fetchData = async ()=>{ | ||
setLoading(true); | ||
try{ | ||
const response = await fetch(url) | ||
const data = await response.json() | ||
setProducts(data) | ||
}catch(error){ | ||
setError(error) | ||
}finally{ | ||
setLoading(false) | ||
} | ||
} | ||
fetchData() | ||
}, []) | ||
|
||
return {loading, products, error} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
export default useData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters