-
Notifications
You must be signed in to change notification settings - Fork 10.3k
docs(gatsby): Add documentation for useStaticQuery #11741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dc7dbd7
Add documentation for useStaticQuery
sidharthachatterjee 8ad8a57
Change casing of title for consistency with other docs
sidharthachatterjee ad14244
Update docs/docs/use-static-query.md
DSchau 99ed6ef
Update docs/docs/use-static-query.md
DSchau b75b7f4
Update docs/docs/use-static-query.md
DSchau 1bd28b7
Update docs/docs/use-static-query.md
DSchau cee8ded
Add link to StaticQuery
sidharthachatterjee e39b0f3
Update docs/docs/use-static-query.md
pieh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| --- | ||
| title: Querying data in components with the useStaticQuery hook | ||
| --- | ||
|
|
||
| Gatsby v2.1.0 introduces `useStaticQuery`, a new Gatsby feature that provides the ability to use a [React Hook](https://reactjs.org/docs/hooks-intro.html) to query with GraphQL at _build time_. | ||
|
|
||
| Just like the [StaticQuery](/docs/static-query/) component, it allows your React components to retrieve data via a GraphQL query that will be parsed, evaluated, and injected into the component. However, `useStaticQuery` is a hook rather than a component that takes a render prop! | ||
|
|
||
| In this guide, we'll walk through an example using `useStaticQuery`. If you're not familiar with static queries in Gatsby, you might want to check out [the difference between a static query and a page query](/docs/static-query/#how-staticquery-differs-from-page-query). | ||
|
|
||
| ## How to use useStaticQuery in components | ||
|
|
||
| > 💡 You'll need React and ReactDOM 16.8.0 or later to use `useStaticQuery`. | ||
| > | ||
| > 📦 `npm install react@^16.8.0 react-dom@^16.8.0` | ||
|
|
||
| `useStaticQuery` is a React Hook. All the [Rules of Hooks](https://reactjs.org/docs/hooks-rules.html) apply. | ||
|
|
||
| It takes your GraphQL query and returns the requested data. That's it! | ||
|
|
||
| ### Basic example | ||
|
|
||
| Let's create a `Header` component that queries for the site title from `gatsby-config.js`: | ||
|
|
||
| ```jsx:title=src/components/header.js | ||
| import React from "react" | ||
| import { useStaticQuery, graphql } from "gatsby" | ||
|
|
||
| export default () => { | ||
| const data = useStaticQuery(graphql` | ||
| query HeaderQuery { | ||
| site { | ||
| siteMetadata { | ||
| title | ||
| } | ||
| } | ||
| } | ||
| `) | ||
|
|
||
| return ( | ||
| <header> | ||
| <h1>{data.site.siteMetadata.title}</h1> | ||
| </header> | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| ## Known Limitations | ||
pieh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| - `useStaticQuery` does not accept variables (hence the name "static"), but can be used in _any_ component, including pages | ||
| - Because of how queries currently work in Gatsby, we support only a single instance of `useStaticQuery` in a file | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.