Display GraphQL Data with a React Component

Ian Jones
InstructorIan Jones

Share this video with your friends

Send Tweet
Published a year ago
Updated 10 months ago

We take the data from our useQuery result and display it in an unordered list element.

We need to make sure to return a loading indicator when result.data is undefined. When result.data is truthy, we know that the GraphQL request has returned the data we asked for.

Instructor: [0:00] Now that we're getting comments, let's render them to the page. Down in our comments component, let's remove this console.log. Instead of returning null, we'll return an unordered list. We will go result.data, then GitHub.repository.issue comments.nodes. We know nodes is an array.

[0:37] We can map for each comment node. We will return an li with comment node.body, type error, "You cannot read property GitHub of undefined." We're getting this error because on our first render, we're trying to access data that useQuery hasn't returned anything yet, so we need to check against this.

[1:15] If not result.data return loading. Now we get loading. There's all of our comments. Looks like we forgot a key, which is important for React rendering. I add key = {comment.node.id}.