【问题标题】:Cannot fetch images by setting State无法通过设置状态获取图像
【发布时间】: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 错误:访问令牌无效

标签: reactjs setstate


【解决方案1】:
...
.then((results) => {
   setPics(response.results);
});
...

在这里,您传递了results,但尝试设置不存在的response。把它改成这样:

...
.then((results) => {
   setPics(results?.response?.results || []); // from your image
});
...

【讨论】:

    【解决方案2】:

    我试过了,成功了-

    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: "Yme6ZcumIXpWryQ0DPc249CE0ua2Mxh66Y-4W2gPAAc"
      });
    
      const searchPhotos = async (e) => {
        e.preventDefault();
        unsplash.search
          .getPhotos({
            query: query
          })
          .then((results) => {
            setPics(results.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>{" "}
        </>
      );
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 1970-01-01
    相关资源
    最近更新 更多