React - TypeError无法读取未定义的属性“map”

问题描述 投票:0回答:1

我在搜索栏中输入用户名时遇到问题。问题是,如果我们输入用户名并想要从搜索栏中删除它,则在Sand Box中发生错误TypeError Cannot read property 'map' of undefined,但是,我的计算机(本地)TypeError: this.props.users.items is undefined上发生错误。这是我的代码

import React, { Component } from "react";
import User from "./ItemUser";

class UsersList extends Component {
  get users() {
    return this.props.users
      ? this.props.users.items.map(user => <User key={user.id} user={user} />)
      : null;

  }

  render() {
    return <div>{this.users}</div>;
  }
}

export default UsersList;
reactjs react-router
1个回答
1
投票

您确定您的用户有商品属性吗?现在你只是检查this.props.users是否存在,添加一个额外的检查,看看这些用户是否有这样的项目可能会有所帮助:

this.props.users && this.props.users.items ? ...

我假设错误来自UsersList组件。如果不是这样,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.