Gatsby Tutorial #10 - Static Queries (useStaticQuery)
Skills:
AI Workflow Automation80%
Key Takeaways
Makes static queries in Gatsby components using useStaticQuery
Full Transcript
all right then gang so we've seen how to make a query inside a page this is a page query we export this query at the bottom then we can use that data in our component but how do we then also make a query inside a component that is not a page we can't do it the same way if we tried this kind of thing inside one of these components it would not work instead what we have to do is use something called a static query and that's a bit different from a page query first i'm going to show you how to do it so what i'd like to do is make a query inside the nav bar over here now what i'm going to do is import a hawk in order to make this query and that comes from gatsby so let me import a couple of things first of all i'm going to import graphql because we also need to import that we're going to still make a query using graphql but also a hook called use static query like so and this is the hook we use to make a query inside the component this time we don't do it outside the component we do it inside the component so the way we do this is by saying const and then data is equal to use static query and then inside here we say graphql like so and then we do our template string again the backticks and then we just pass in our query as normal so what i'm going to do is grab this query over here because it's going to be almost identical i'm going to cut that and i'm going to paste it over here so i'm going to still call it site info and this time i don't want the description i just want the title so let me get rid of the description right there so what i'm going to do now is use this data inside this component because this returns the data right here before i do that i want to destructure the title from the data so i'm going to say const and then title and that is equal to data which is this thing right here then we need to go on to the site property which is this so dot site then the site metadata so dot site meta data like so so we're grabbing the title from that and then we can use the title inside this component and i'm going to do that right here instead of web warrior so it's not hard coded anymore and the title is now dictated by gatsby config over here it is the same in this case but we can change it to something different if we wanted to so i'm going to now save this file over here and i'm also going to go back to the index file and i'm going to get rid of this page query because i'm not going to use that anymore and i can get rid of this where i output it um we don't need this at the top because we're no longer getting that data and we can get rid of it right here we just now have it inside the nav bar over here so if we go over here i do see an error so i'm just going to refresh to see if the error goes away yes it does and now we can see inside the navbar we have web warrior okay so it still outputs it it's just now being driven by that dynamic data and a query that we make right here now i said before that we technically don't need to have this bit inside the query i just want to show you that if i delete this it doesn't matter that it's not here anymore so if i save it and come over here i can refresh it's still going to work exactly the same way now there is a difference between these static queries right here and the page queries at the bottom of page components and that is in page components we can use query variables now we've not seen those yet but you'll see them later and in these queries you can't use query variables so just be aware of that one other thing i also want to mention is that this hook right here sometimes it can be a bit temperamental and throw an error one thing i've noticed is that if i use components that are not capitalized right here this sometimes throws an error and it doesn't really like it so i always make sure these are capitalized so i don't get that error now i'm not done much digging around with this it's just my recent experience using it and finally one more thing you can only use this hook once inside a component so you can't use this hook again down here that won't work so now we know how to make page queries and we also know how to make static queries inside components that are not pages next up we're going to start to look at adding other data sources using our file system
Original Description
Hey gang, in this Gatsby tutorial I'll show you how to make static queries in components that are not site pages, using the useStaticQuery hook.
🐱💻 🐱💻 Course Files:
+ https://github.com/iamshaunjp/gatsby-tutorial
🐱👤🐱👤 JOIN THE GANG -
https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join
🐱💻 🐱💻 My Udemy Courses:
+ Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript
+ Vue JS 3 & Firebase - https://www.thenetninja.co.uk/udemy/vue-and-firebase
+ D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase
🐱💻 🐱💻 Useful playlists:
+ Full React tutorial - https://www.youtube.com/watch?v=j942wKiXFu8&list=PL4cUxeGkcC9gZD-Tvwfod2gaISzfRiP9d
+ GraphQL tutorial - https://www.youtube.com/watch?v=Y0lDGjwRYKw&list=PL4cUxeGkcC9iK6Qhn-QLcXCXPQUov1U7f
🐱💻 🐱💻 Other links:
+ Gatsby docs - https://www.gatsbyjs.com/docs/
+ Markdown guide - https://guides.github.com/features/mastering-markdown/
+ Download Git - https://git-scm.com/downloads
+ Get VS Code - https://code.visualstudio.com/
🐱💻 🐱💻 Social Links:
Facebook - https://www.facebook.com/thenetninjauk
Twitter - https://twitter.com/thenetninjauk
Instagram - https://www.instagram.com/thenetninja/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
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
30
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
More on: AI Workflow Automation
View skill →Related Reads
📰
📰
📰
📰
Why Vanilla JS?
In the article below, I am sharing my story of building SaaS product in vanilla js and explaining why I decided to go with this approach.
https://guseyn.com/html/posts/why-vanilla-js.html
Dev.to · Guseyn Ismayylov
How to Create a Cursor Tail Using HTML, CSS, and JavaScript
Medium · JavaScript
I built a landing page with Three.js, vanilla JS, and zero frameworks — here's what I learned
Dev.to · Ayush Shekhar
Part 1: Why I Rarely Use useEffect Anymore (and what I use instead)
Dev.to · Alejandro
🎓
Tutor Explanation
DeepCamp AI