无法使用 API 渲染图像

编程入门 行业动态 更新时间:2024-10-04 17:20:15

无法使用 API 渲染<a href=https://www.elefans.com/category/jswz/34/1771430.html style=图像"/>

无法使用 API 渲染图像

我正在使用多头像 api 在 UI 上呈现随机图像,但出现以下错误。我也试过使用 promises 来渲染 UI 但没有得到结果。

未捕获的类型错误:第一个参数必须是字符串类型之一, Buffer、ArrayBuffer、Array 或 Array-like Object。接收类型 未定义

import React, { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import { ToastContainer, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import axios from "axios";
import { profilePicRoute } from "../utils/apiRoutes";
import { Buffer } from "buffer";

function ProfilePic() {
  const api = "";
  const navigate = useNavigate();
  const [profilePic, setProfilePic] = useState([]);
  const [isLoading, setIsLoading] = useState(true);
  const [selectedPofilePic, setSelectedPofilePic] = useState(undefined);

  const toastStyles = {
    position: "top-center",
  };

  const setProfilePicture = async () => {};
  useEffect(() => {
    const data = [];
    for (let i = 0; i < 4; i++) {
      const image = axios.get(`${api}/${Math.round(Math.random() * 1000)}`);
      const buffer = Buffer(image.data);
      data.push(buffer.toString("base64"));
      console.log(data);
    }
    setProfilePic(data);
    setIsLoading(false);
  }, []);

  return (
    <div className="profilePage">
      <h1>Pick your favorite profile picture</h1>
      <div className="profilePics">
        {profilePic.map((pic, index) => {
          return (
            <div
              key={index}
              className={`pic ${selectedPofilePic === index ? "selected" : ""}`}
            >
              <img
                src={`data:image/svg+xml;base64,${pic}`}
                alt="profile pic"
                onClick={() => setSelectedPofilePic(index)}
              />
            </div>
          );
        })}
      </div>
      <ToastContainer />
    </div>
  );
}

export default ProfilePic;
回答如下:
Since you were using the async you must have to use await keyword , otherwise it will return promises,and you should use the function inside the useEffect 

import React, { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import { ToastContainer, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import axios from "axios";
import { profilePicRoute } from "../utils/apiRoutes";
import { Buffer } from "buffer";

function ProfilePic() {
  const api = "https://api.multiavatar";
  const navigate = useNavigate();
  const [profilePic, setProfilePic] = useState([]);
  const [isLoading, setIsLoading] = useState(true);
  const [selectedPofilePic, setSelectedPofilePic] = useState(undefined);

  const toastStyles = {
    position: "top-center"
  };

  useEffect(() => {
    const setProfilePicture = async () => {
      const data = [];
      for (let i = 0; i < 4; i++) {
        const image = await axios.get(
          `${api}/${Math.round(Math.random() * 1000)}`
        );
        console.log(image);
        const buffer = Buffer(image.data);
        data.push(buffer.toString("base64"));
      }
      setProfilePic(data);
      setIsLoading(false);
    };
    setProfilePicture();
  }, []);

  return (
    <div className="profilePage">
      <h1>Pick your favorite profile picture</h1>
      <div className="profilePics">
        {profilePic.map((pic, index) => {
          return (
            <div
              key={index}
              className={`pic ${selectedPofilePic === index ? "selected" : ""}`}
            >
              <img
                src={`data:image/svg+xml;base64,${pic}`}
                alt="profile pic"
                onClick={() => setSelectedPofilePic(index)}
              />
            </div>
          );
        })}
      </div>
      <ToastContainer />
    </div>
  );
}

export default ProfilePic;

Hope this code will help you.
Happy Coding :)

更多推荐

无法使用 API 渲染图像

本文发布于:2024-05-31 01:41:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771143.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!