简体   繁体   English

为什么我在使用 useState 作为数组时出现错误

[英]why i am getting an error while i am using useState as an array

import axios from "axios";
import { useEffect, useState } from "react";
import "./styles.css";
export default function App() {
  const [userDataInfo, setUserDataInfo] = useState([]);
  const fetchData = () => {
    axios
      .get("https://random-data-api.com/api/address/random_address")
      .then(function (response) {
        // console.log(response.data);
        return JSON.stringify(response);
      })
      .then(function (data) {
        setUserDataInfo(data);
      });
  };
  useEffect(() => {
    fetchData();
  }, [userDataInfo]);
  return (
    <div className="App">
      {userDataInfo.map((data) => (
        <p>{}</p>
      ))}
    </div>
  );
}
TypeError
userDataInfo.map is not a function
App
/src/App.js:26:20
  23 | 
  24 | return (
  25 |   <div className="App">
> 26 |     {userDataInfo.map((data) => (
     |                  ^
  27 |       <p>{}</p>
  28 |     ))}`enter code here`
  29 |   </div>

Generally, the map() method is used to iterate over an array and call a function on every element of the array and if I am using userDataInfo as an array then why I am getting this error?通常,map() 方法用于遍历数组并在数组的每个元素上调用一个函数,如果我使用 userDataInfo 作为数组,那么为什么会出现此错误?

 const [userDataInfo, setUserDataInfo] = useState([]);

userDataInfo starts out as an array. userDataInfo以数组开始


 .get("https://random-data-api.com/api/address/random_address")

But then you get that URL which returns a JSON representation of an object (not an array).但随后您会得到返回对象(不是数组)的 JSON 表示形式的 URL。


 return JSON.stringify(response);

And then you convert the object into a string of JSON before storing that string in setUserDataInfo .然后将对象转换为 JSON 字符串,然后将该字符串存储在setUserDataInfo中。


  • Don't convert your data to JSON.不要将您的数据转换为 JSON。 JSON is a format useful for sending to external places through APIs (like fetch or localStorage ). JSON 是一种可用于通过 API(如fetchlocalStorage )发送到外部位置的格式。 It isn't useful for processing data structures internally.它对于内部处理数据结构没有用。
  • Do decide what format of data you want to store in userDataInfo .确定要存储在userDataInfo中的数据格式。 If you are going to store multiple addresses in there, then use an array (and change your handling of the data from the URL to reflect that … eg by adding to an existing array instead of overwriting with a single value).如果您要在其中存储多个地址,则使用一个数组(并更改您对来自 URL 的数据的处理以反映这一点……例如,通过添加到现有数组而不是用单个值覆盖)。 If you are going to use only a single item, then only store a single item (and get rid of the map ).如果您打算只使用一个项目,那么只存储一个项目(并摆脱map )。

response object would be in the following format响应对象将采用以下格式

{
data:"some data"
}

assuming that the api request returns an array then all you need to do is:假设 api 请求返回一个数组,那么您需要做的就是:

axios
      .get("https://random-data-api.com/api/address/random_address")
      .then(function (response) {
        setUserDataInfo(response.data);
      })

perhaps take a look at the axios documentation if need further clarification on the response schema below:如果需要进一步澄清下面的响应模式,也许可以查看 axios 文档:

https://axios-http.com/docs/res_schema https://axios-http.com/docs/res_schema

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在angular js中使用resolve时出现错误? - why i am getting error while using resolve in angular js? 为什么在抓取JavaScript时出现500错误 - Why am I am getting 500 Error while crawling in javascript "为什么这个多维数组出现错误?" - Why am I getting an error with this multidimensional array? 为什么我在使用 javascript 数组中的 .length 属性时收到此错误? - Why I am getting this error using the .length property in javascript array? 我在使用 nodemailer 时遇到错误 - i am getting error while using nodemailer 为什么我收到“未捕获的错误:重新渲染次数过多”。 在尝试使用 useState 将一个从自定义挂钩中获得的常量分配给另一个常量时? - Why I am getting 'Uncaught Error: Too many re-renders.' while a try to assign a const that a got from custom hook into another const using useState? 使用 reactjs 钩子时,为什么会出现“TypeError: react__WEBPACK_IMPORTED_MODULE_1___default.a.useState is not a function”错误? - Why am I getting "TypeError: react__WEBPACK_IMPORTED_MODULE_1___default.a.useState is not a function" error when using reactjs hooks? 为什么会出现此错误? - Why am I getting this error? 为什么在获取数组作为道具时,map 方法出现未定义错误? - Why am I getting undefined error for map method while getting array as a prop? 为什么在重新计算数组时会得到增值? - Why am I getting value increase while recalculating an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM