【发布时间】:2020-04-29 03:15:49
【问题描述】:
我正在尝试显示来自 dog API URL 的随机图像。
当前显示的是损坏的图像。我正在使用 Axios 和 React,目前在 eslint 中没有遇到任何错误/问题。
我想单击按钮以显示来自 API 的随机图像,但它只是显示带有损坏图像的 alt 文本。
代码
import * as React from "react";
import { render } from "react-dom";
import axios from "axios";
import "./styles.css";
function App() {
const [image, getImage] = React.useState("");
function btnClick(event) {
event.preventDefault();
axios
.get("https://dog.ceo/api/breeds/image/random")
.then(response => {
getImage({ imgs: response.message });
})
.catch(err => {
console.log("Error happened during fetching!", err);
});
}
return (
<div className="App">
<img src={image} alt="broken"/>
<button className = "Button" onClick = {btnClick}>Click</button>
</div>
);
}
const rootElement = document.getElementById("root");
render(<App />, rootElement);
【问题讨论】:
标签: reactjs typescript axios react-hooks