GraphQL Tutorial #27 - Rendering Data in a Component
Key Takeaways
Renders data in a component in GraphQL to display query results
Full Transcript
alright then so in the last tutorial we saw the props of the component output twice to the console over here right let me just zoom in so we can see and we saw that the second time around the books were attached to this data property inside the props so what happens with react is that when some kind of data or the props or something to do with the component updates then react re-renders the component right so then we can match the component with the data so when we receive the data here the books on the props we can use that data to out pull it to the screen over here so why don't we renders the books are gonna be listed here makes sense okay cool so that's what we're gonna do in this video so let's head over to the code I'm inside the book list component here and what I'd like to do is first of all get rid of this consultant log inside the render function I'd like to create another function which is going to control the output of the book data to the screen in the component alright so let's create that function right here we'll call the function display books that makes sense alright so inside this function what do we want to do well first of all let's create a variable called data and set that equal to this dot props dot data right remember when we output this to the console it's all attached to a data property on the props that's what graph QL does it attaches it to the data property right here all right notice also that the first time around this loading property right here is true that means that the query is still loading in the background it's not returned to us yet we don't have that data yet whereas down here the second time it's logged out loading is false so obviously we have data at that point it's no longer loading and we do right here we have the array of books so what we could do is use this property loading to check if the data is actually ready for us yet right and if it is ready then we'll output the books if it's not ready then we'll output something else like loading books or something like that so let's head to the code again and here let's do that little check will say if data loading remember that's the loading property so if this dot product that is true then what do we want to do well we can output the books yet so let's just output something else like a loading books so we'll return something here and it's just gonna be a div tag and this div tag is going to say something like loading books like so all right let's close that div tag so that's what we're gonna do in this case now then if this is false then we're going to run the else statement and that means if loading is false that we have the data right so now what we can do is take that data take the books and we can map them to some hasty ml and output them so what we'll do in this case is return again and we'll return data dots books because now we have access to the books property on the data remember data dot books here so that is an array so therefore we can map that array to some HTML so what map does is go through that array and give us access to the individual item each time around which I'm going to call bulk so this is going to be an error function so we're taking that book of firing a function each time around okay and each time around each iteration through that array for each item will return some HTML so return and then in parentheses we'll do an li tag and inside the Li tag we'll do book dot name all right so let's spell it correctly book name let me just explain this briefly again once I'm done here if loading is true then we're just gonna show this loading books in loading is false we'll fire this and we're returning data box map which is cycling through that array and each iteration through when it lands on each item in the array we're taking that book and we're using it inside this es6 function right here which each time around is returning some HTML and it's taking that that book and grabbing the name property off it and placing it inside the LI tag right here remember the curly braces is how we output data dynamically in react now let's give this a whirl there's something missing but I'm going to come back to that in a second in fact what we need to do is call this function otherwise nothing will happen so down here instead of this Li what we need to do is open our curly braces and inside the curly braces call the function so we say this dots display books all right so this refers to the component then dot display books which is the method inside this component it's going to run this and either show us this or this stuff so let's save it and find out what happens let's go to the browser and you can see right here that we have all of the books on the screen now if we refresh this if you're really quick you might see the loading bit at the start so refresh loading books very quickly it goes once the query comes back with the data and then we have this stuff output to the browser that is awesome now I said there was one thing left to do and it's because we have this error right here which basically says each child in an array or iterator should have a unique key prop so if we go to the code right here it's saying that each one of these Li tags the way I'll put in has to have some kind of key some kind of identifier for each Li tag so react knows exactly what it is if you like all right so we can say that the key is going to be equal to the book dots ID right and that's going to be unique for each different Li type because in the query up here we say we want the name of each book and the ID of each book so I'll put in a name within the Li tag and the ID of the book as the key and that will be different for each different Li tag that we iterate through so let's save this and make sure that the the error goes away if we refresh we can see loading then this flash is off and the error is gone so awesome well done that is how we can bind our query with the component and then have access to all of the data that comes back from that query and output it to the screen so we're getting closer and closer to the final product here we're starting to interact with our graph you are server and in the next tutorial what we're going to do is start to create different components to add data to this list so I'll see you in that next tutorial
Original Description
Hey gang, in this GraphQL tutorial we'll be seeing how we can render the data we received from our query bound to the component.
DONATE :) - https://www.paypal.me/thenetninja
----- COURSE LINKS:
+ Course files - https://github.com/iamshaunjp/graphql-playlist
+ Atom editor - https://atom.io/a
+ CMDER - http://cmder.net/
+ Node Download - https://nodejs.org/en/
======== Other Tutorials =========
----- NODE.JS TUTORIALS
https://www.youtube.com/playlist?list=PL4cUxeGkcC9gcy9lrvMJ75z9maRw4byYp
----- MONGODB TUTORIALS
https://www.youtube.com/playlist?list=PL4cUxeGkcC9jpvoYriLI0bY8DOgWZfi6u
----- REACT TUTORIALS
https://www.youtube.com/playlist?list=PL4cUxeGkcC9i0_2FF-WhtRIfIJ1lXlTZR
----- SUBSCRIBE TO CHANNEL - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg?sub_confirmation=1
======== Social Links ==========
Twitter - @TheNetNinja - https://twitter.com/thenetninjauk
Patreon - https://www.patreon.com/thenetninja
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 30 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
▶
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
Related AI Lessons
⚡
⚡
⚡
⚡
Common Next.js Errors (and How I Solved Them)
Dev.to · gary killen
Applying Scalability in Backend (CodeBuddy)
Medium · LLM
Why Every Backend Developer Should Learn Nginx Before Going to Production
Medium · DevOps
Connecting Frontend to Backend: A Backend Engineer’s Reality Check
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI