【发布时间】:2021-12-23 06:16:33
【问题描述】:
我正在尝试通过设置 const [pics, setPics] = useState([]) 来显示我的图片。在 searchPhotos 函数中,我不知道如何将 setPics 的状态设置为从 API 密钥获得的结果。我遇到了错误:“'response' is not defined no-undef”。请帮我解决我的问题!非常感谢!
App.js:
import React, { useState } from "react";
import { createApi } from "unsplash-js";
export default function App() {
const [query, setQuery] = useState("");
const [pics, setPics] = useState([]);
const unsplash = createApi({
accessKey: "//my config"
});
const searchPhotos = async (e) => {
e.preventDefault();
unsplash.search
.getPhotos({
query: query
})
.then((results) => {
setPics(response.results);
});
};
return (
<>
<form className="form" onSubmit={searchPhotos}>
<label className="label" htmlFor="query">
{" "}
????{" "}
</label>{" "}
<input
type="text"
name="query"
value={query}
onChange={(e) => setQuery(e.target.value)}
className="input"
placeholder={`Try "dog" or "apple"`}
/>{" "}
<button type="submit" className="button">
Search{" "}
</button>{" "}
</form>
<div className="card-list">
{" "}
{pics.map((pic) => {
return (
<div className="card" key={pic.id}>
<img
className="card--image"
alt={pic.alt_description}
src={pic.urls.full}
width="50%"
height="50%"
></img>{" "}
</div>
);
})}{" "}
</div>{" "}
</>
);
}
沙盒链接以便更好地观察:https://codesandbox.io/s/unsplash-search-oshcc?file=/src/App.js
【问题讨论】:
-
我可以看到一个错误:OAuth 错误:访问令牌无效