<html>
<head>
<meta charset="UTF-8" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
<div id="app"></div>
<script type="text/babel">
const { useState, useEffect } = React
function App() {
const [data, setData] = useState(null);
const [error, setError] = useState(null);
useEffect(() => {
fetch('https://api.coindesk.com/v1/bpi/currentprice.json')
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error('Error: ' + response.status);
}
})
.then(data => {
setData(data.bpi);
})
.catch(error => {
setError(error.message);
});
}, []);
if (error) {
return <div>Error: {error}</div>;
}
if (!data) {
return <div>Loading...</div>;
}
// Render your filtered data
return (
<div className='container'>
<h3>React Js Fetch data from API using fetch() API</h3>
<table>
<thead>
<tr>
<th>Currency</th>
<th>Rate</th>
</tr>
</thead>
<tbody>
{Object.keys(data).map(currency => (
<tr key={currency}>
<td>{currency}</td>
<td>{data[currency].rate}</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.container {
text-align: center;
padding: 20px;
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24);
margin: 0 auto;
width: 600px
}
/* Table styles */
table {
width: 100%;
border-collapse: collapse;
font-family: Arial, sans-serif;
}
/* Table header styles */
thead {
background-color: #f2f2f2;
}
thead th {
padding: 10px;
font-weight: bold;
border-bottom: 1px solid #ddd;
}
/* Table body styles */
tbody tr:nth-child(even) {
background-color: #f9f9f9;
}
tbody td {
padding: 10px;
border-bottom: 1px solid #ddd;
}
/* Hover effect */
tbody tr:hover {
background-color: #e5e5e5;
}
</style>
</body>
</html>