请给我写一个HTML和JavaScript,要求必须使用react框架,要求必须使用react-query库,发送一个异步请求去访问后端接口的数据。后端接口的地址是 api.exchangerate.host/latest
时间: 2023-09-23 10:05:49 浏览: 114
### 回答1:
<html>
<head>
<title>React Query App</title>
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/@tanem/react-query/dist/react-query.umd.js"></script>
</head>
<body>
<div id="root"></div>
<script>
const { useQuery } = ReactQuery; const ExchangeRate = () => {
const { status, data, error } = useQuery('exchangeRate', async () => {
const res = await fetch('api.exchangerate.host/latest');
return res.json();
}); if (status === 'loading') return <p>Loading exchange rate...</p>;
if (status === 'error') return <p>Error: {error.message}</p>; return <p>Exchange rate: {data.rate}</p>;
}; const App = () => (
<React.Fragment>
<h1>React Query App</h1>
<ExchangeRate />
</React.Fragment>
); ReactDOM.render(<App />, document.getElementById('root'));
</script>
</body>
</html>
### 回答2:
以下是一个使用React框架和react-query库发送异步请求访问后端接口数据的HTML和JavaScript代码示例:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>React Query Example</title>
</head>
<body>
<div id="root"></div>
<script src="https://unpkg.com/react/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/react-query/dist/react-query.production.min.js"></script>
<script>
const { useState } = React;
const { QueryClient, QueryClientProvider, useQuery } = ReactQuery;
const queryClient = new QueryClient();
function App() {
const { isLoading, error, data } = useQuery("exchangeRate", fetchExchangeRate);
if (isLoading) return <div>Loading...</div>;
if (error) return <div>An error occurred: {error.message}</div>;
return (
<div>
<h1>Exchange Rate</h1>
<p>USD to EUR: {data.rates.EUR}</p>
<p>USD to GBP: {data.rates.GBP}</p>
</div>
);
}
async function fetchExchangeRate() {
const response = await fetch("https://api.exchangerate.host/latest");
const data = await response.json();
return data;
}
ReactDOM.render(
<QueryClientProvider client={queryClient}>
<App />
</QueryClientProvider>,
document.getElementById('root')
);
</script>
</body>
</html>
```
该代码在HTML页面中使用了React、ReactDOM和react-query的CDN链接。它定义了一个名为App的组件,使用useQuery钩子来发送带有标识符"exchangeRate"的异步请求。fetchExchangeRate函数通过异步方式从"https://api.exchangerate.host/latest"地址获取数据。当数据加载中时,页面显示"Loading...";当发生错误时,显示错误信息;当数据加载完成时,显示USD到EUR和USD到GBP的汇率信息。
以上代码中包含了使用React、ReactDOM和react-query库的基本用法,可在浏览器中运行并查看结果。
### 回答3:
以下是使用React和react-query库发送异步请求访问后端接口数据的示例代码:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>React App</title>
</head>
<body>
<div id="root"></div>
<script src="https://unpkg.com/react@17.0.2/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/react-query/dist/react-query.production.min.js"></script>
<script src="./app.js" type="module"></script>
</body>
</html>
```
```javascript
// app.js
const { useState } = React;
const { useQuery } = window.ReactQuery;
const fetchData = async () => {
const response = await fetch('https://api.exchangerate.host/latest');
const data = await response.json();
return data;
};
function App() {
const { data, error, isError, isLoading } = useQuery('exchangeRate', fetchData);
if (isLoading) {
return <div>Loading...</div>;
}
if (isError) {
return <div>Error: {error.message}</div>;
}
return (
<div>
<h1>Exchange Rates</h1>
<ul>
{Object.entries(data.rates).map(([currency, rate]) => (
<li key={currency}>
{currency}: {rate}
</li>
))}
</ul>
</div>
);
}
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
```
这个示例使用了React函数组件来创建一个简单的应用,并使用react-query库的useQuery钩子函数来发送异步请求访问后端接口的数据。请求的URL是`api.exchangerate.host/latest`。页面会展示从接口返回的汇率数据。
注意,在上述代码中需要替换`<script src="./app.js" type="module"></script>`中的`./app.js`为实际的脚本文件路径。
阅读全文