Reputation: 363
I'm trying to create an application in ReactJs based on GitHub Api.
I get this error this.props.users.items is undefined
. This is my Api link https://api.github.com/search/users?q=A and I see it so that there is an object and in it an array of items. How to refer to this api to get a list of users?
This is UserList component
import React, { Component } from 'react';
import User from "./ItemUser"
class UsersList extends Component {
get users() {
return this.props.users.items.map(user => <User key={user.id} user={user}/>);
}
render() {
return (
<div>
{ this.users }
</div>
);
}
}
export default UsersList;
Upvotes: 0
Views: 328
Reputation: 481
Since you are updating state asynchronously
the initial state ie: {} an empty Object is passed as props.
Set initial state as null and in render check whether it is updated or not and return the element
....
this.state = {
users: null
};
....
render() {
....
{this.state.users ? (
<UsersList users={this.state.users}/>
) : (
null
// or a loading placeholder
)}
....
}
Upvotes: 3