简体   繁体   English

“类型错误:种类。map 不是 function。” 反应.js

[英]“TypeError: kinds.map is not a function.” React.js

I'm trying to get data from an API and return it as a 'state' inside a component which is Navbar component but when i try to loop inside the data i get from the API, using 'map' i get an following error我正在尝试从 API 获取数据并将其作为导航栏组件内的“状态”返回,但是当我尝试在从 API 获取的数据中循环时,使用“地图”我收到以下错误

"TypeError: kinds.map is not a function." “类型错误:种类。map 不是 function。”

I can console.log the data which is following but can't display in my component this is the data i get from my browser console:我可以控制台记录以下数据,但无法在我的组件中显示这是我从浏览器控制台获取的数据:

 [["1", "Electronic"], ["2", "Clothes"], ["3", "Beauty"], ["4", "Phone Cases"]]

My only component is following:我唯一的组件如下:

function getProductKinds(callBack) {
  var url = "http://localhost:8000/api/products";
  const responseType = 'json';
  const method = "GET";
  const xhr = new XMLHttpRequest();

  xhr.open(method, url);

  xhr.onload = () => {
    //console.log(xhr.response, xhr.status);
    callBack(xhr.response, xhr.status);
  };

  xhr.send();
}

function NavbarComponent(props) {
  const [kinds, setKinds] = useState([]);
  useEffect(() => {
    const myCallBack = (response, status) => {
      console.log("received data: ", response, status);
      if (status === 200) {
        setKinds(response);
        console.log("received data2: ", kinds, status);
      }
    };
    getProductKinds(myCallBack);
  }, []);
  return (
    <div>
      <Navbar color="light" light expand="md">
        <NavbarBrand href="/">BRAND</NavbarBrand>
        <Nav className="mx-auto " navbar>
        {kinds.map((kind) => {
            <NavItem>ss</NavItem>;
          })}
        </Nav>
        <NavbarText>Simple Text</NavbarText>
      </Navbar>
    </div>
  );
}

export default NavbarComponent;

and the function App is following code: function App 的代码如下:

import './App.css';
import React from 'react';

import { useEffect } from 'react';
import NavbarComponent from './components/navbar';

function App() {

  return (
        <NavbarComponent></NavbarComponent>
  );
}

export default App;

我的浏览器上的字符串输出的屏幕截图

First try to use JSON.parse(response).map to convert that 2D array to array of objects then return the NavItem inside the map callback:首先尝试使用JSON.parse(response).map将该二维数组转换为对象数组,然后在 map 回调中返回NavItem

setKinds(JSON.parse(response).map((curr)=>{
    return {id:curr[0],name:curr[1]}
}))

and

{kinds.map((kind,index)=>{

   return <NavItem key={index}>{kind.name}</NavItem>
}

When you fetch data from API, it takes a little bit of time.当您从 API 获取数据时,需要一点时间。 But react is not waiting for that process to finish and maybe your kinds array is empty when react is rendering.但是 react 并没有等待该过程完成,并且当 react 正在渲染时,您的 kind 数组可能是空的。 You should try this.你应该试试这个。 Instead of代替

{kinds.map((kind) => {
  <NavItem>ss</NavItem>;
})}

try this..尝试这个..

{kinds && kinds.map((kind) => {
  <NavItem>ss</NavItem>;
})}

this should work这应该工作

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM