简体   繁体   English

为什么我的阵列不使用 map 方法,即使它实际上是一个阵列?

[英]Why won't my array use the map method even though it's actually an array?

So I console.log the state of users after I set it as an array of objects from the github api and it displays as an array.因此,在我将用户的 state 设置为来自 github api 的对象数组后,我 console.log 记录它并显示为数组。 However, I can't use the map function like this (if I decided to use it in a child component using props):但是,我不能像这样使用 map function (如果我决定在使用道具的子组件中使用它):

this.state.users.map((user)=>(
 <UserItem key={user.id} user={user}/>
))

I get an error: "TypeError: this.state.users.map is not a function" And It's pretty confusing because it is an array.我收到一个错误:“TypeError:this.state.users.map 不是函数”而且它很混乱,因为它是一个数组。 It looks like this in the props viewer:在道具查看器中看起来像这样: 阵列用户 As you can see.如你看到的。 Users is an array.用户是一个数组。 I don't understand but I could be missing something very simple from lack of coffee.我不明白,但我可能会因为缺乏咖啡而错过一些非常简单的东西。 Thanks for your help.谢谢你的帮助。 I appreciate it.我很感激。

import React, { Component } from 'react';
import Navbar from './components/layout/Navbar';
// import UserItem from './components/users/UserItem';
import Users from './components/users/Users';
// import './App.css';
import axios from 'axios';

class App extends Component {

  state = {
    users: {},
    loading: false
  }

  async componentDidMount() {
    this.setState(
      {loading: true}
    )
    const res = await axios.get('https://api.github.com/users')

    this.setState(
      { 
        loading: false,
        users: res.data,
      }
    
    )

    // console.log(res.data)
  }

  render() {


    return (
      <React.Fragment>
        <Navbar />
        <div className="container">
          <Users loading={this.state.loading} users={this.state.users} />
        </div>
      </React.Fragment>
    );
  }

}

export default App;

The initial value of this.state.users is an object. this.state.users的初始值为 object。 Hence the error map is not a function因此错误map is not a function

state = {
    users: {}, <--- object
    loading: false
}

The API call happens after the component has mounted for the first time in the componentDidMount life cycle method and the value of state.users gets populated with the data. API 调用发生在组件在componentDidMount生命周期方法中首次挂载之后,并且state.users的值被填充了数据。

The component has to render with the initial state on the first render.该组件必须在第一次渲染时使用初始 state 进行渲染。

Change the value of users to be an empty array in the initial state.在初始 state 中将users的值更改为空数组。

state = {
    users: [],  // change initial value of users to be an empty array
    loading: false
}

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

相关问题 为什么 map 方法不会在我的跨度内返回数组项? -反应 - why map method won't return array items inside my span? -REACT 为什么我的数组没有更新,即使它在我 console.log() 时更新 - Why isn't my array updating even though it updates when I console.log() 为什么即使我将它转换为数组,我的 querySelectorAll 也不起作用? - Why is my querySelectorAll not working even though I converted it to an array? 为什么我的比较数组不起作用? - Why won't my comparison array work? 为什么我的数组不会回到 0 (javascript)? - why my array won't back to 0 (javascript)? .map()未定义,即使我传递了数组 - .map() undefined even though I pass an array 即使我有一个数组,map 也不是 function - map is not a function even though I have an array 为什么我的 SCSS 无法加载,即使它位于我使用 React.js 搭建的 Laravel 项目中的 webpack.mix.js 中? - Why won't my SCSS load even though it's in the webpack.mix.js in my Laravel project that's scaffolded with React.js? `document.getElementsByClassName`的结果没有定义像`map`这样的数组方法,即使它是一个数组 - Result of `document.getElementsByClassName` doesn't have array methods like `map` defined, even though it is an array 即使未输入映射循环,React也会在array.map上崩溃 - React crashes on array.map even though the map loop isn't being entered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM