0

I am trying to display a list of user repositories. Through the spread operator attempts to spell the object. However, I do not know if this is a good method, there are no errors in the console, but nothing appears on the screen. This is my code.

class ItemUserDetail extends React.Component {
  constructor() {
    super();
    this.state = {
      usersRepos: []
    };
  }

  componentDidMount() {
    const { user } = this.props.match.params;
    const url = `https://api.github.com/users/${user}/repos`;
    fetch(url)
      .then(res => res.json())
      .then(json => this.setState({ usersRepos: json }));
  }

  render() {
    const Repos = this.state.usersRepos ? { ...this.state.usersRepos } : null;
    return (
      <div>
        <p>{Repos.name}</p>
      </div>
    );
  }
}

export default ItemUserDetail;

1 Answer 1

1

Since you are returning an array of repositories, your render method should look like this

render() {
  const Repos = this.state.usersRepos ? this.state.usersRepos : null; // you don't need this
  const { userRepos } = this.state; // destructure 
  return (
      <div>
        {userRepos.map(repo => <p key={repo.id}>{repo.name}</p>)}
      </div>
  );
}
Sign up to request clarification or add additional context in comments.

2 Comments

I get TypeError: userRepos is undefined :< I dont know why
Here is a working example of your code codesandbox.io/s/14r76r1234 Kindly close this question and mark this as your answer if this solves your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.