繁体   English   中英

× TypeError:无法读取未定义的属性“地图”。 发现错误

[英]× TypeError: Cannot read property 'map' of undefined. found error

谁能帮我解决这个问题?

const Users = ({ users, loading }) => {
  if (loading) {
    return <Spinner />
  } else {
    return (
      <div style={userStyle}>
        {users.map((user) => (
          <UserItem key={users.id} user={user} />
        ))}
      </div>
    )
  }
}

在 map 之前使用条件运算符&&

const Users = ({ users, loading }) => {
  if (loading) {
    return <Spinner />
  } else {
    return (
      <div style={userStyle}>
        {users && users.map((user) => (
          <UserItem key={user.id} user={user} />
        ))}
      </div>
    )
  }
}

在 map 内部,您应该使用用户而不是用户,因此代码将是

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

Because, you are passing user as a argument for callback function not users.

暂无
暂无

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

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