React - TypeError Cannot read property 'map' of undefine

时间:2018-09-18 19:59:46

标签: reactjs react-router

I have a problem with entering usernames in the search bar. The problem is that if we enter the username and want to remove it from the search bar, an error occurs TypeError Cannot read property 'map' of undefined in Sand Box, however, an error occurs on my computer (local) TypeError: this.props.users.items is undefined. This is my code

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;

1 个答案:

答案 0 :(得分:1)

Are you certain that your users have an item property? Right now you're just checking to see if this.props.users exists, it might be helpful to add an additional check for whether those users have items like so:

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

I'm assuming that the error is coming from the UsersList component. If that isn't the case, let me know.