screen_rotation
Copied to Clipboard
<!DOCTYPE html> <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> <script src="https://unpkg.com/axios/dist/axios.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(() => { async function fetchData() { try { const response = await axios.get( 'https://api.coindesk.com/v1/bpi/currentprice.json' ); setData(response.data.bpi); } catch (error) { setError(error.message); } } fetchData(); }, []); if (error) { return <div>Error: {error}</div>; } if (!data) { return <div>Loading...</div>; } // Render your filtered data return ( <div className='container'> <h3>React Async Await Handle HTTP Request with 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 } h3 { color: #333; text-align: center; } table { width: 100%; border-collapse: collapse; margin-top: 20px; } th, td { padding: 10px; text-align: left; border-bottom: 1px solid #ddd; } thead th { background-color: #f5f5f5; font-weight: bold; } tbody tr:hover { background-color: #f5f5f5; } </style> </body> </html>