-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
feat(gatsby): add useStaticQuery hook #11588
Conversation
packages/babel-plugin-remove-graphql-queries/src/__tests__/__snapshots__/index.js.snap
Outdated
Show resolved
Hide resolved
packages/babel-plugin-remove-graphql-queries/src/__tests__/__snapshots__/index.js.snap
Outdated
Show resolved
Hide resolved
packages/gatsby/index.d.ts
Outdated
@@ -20,4 +20,6 @@ export interface StaticQueryProps { | |||
|
|||
export class StaticQuery extends React.Component<StaticQueryProps> {} | |||
|
|||
export const useStaticQuery: (query: any) => void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could probably improve typings here since query
needs to be graphql tagged template
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It also should return Object
(definitely not void
, as this would probably make TS complain when using result)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, missed that! It can be null
or obj though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sidharthachatterjee good call! I think I'd just do
export const useStaticQuery: (query: any) => null | object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed the return here
I'll fix the input for both StaticQuery
and useStaticQuery
in a separate PR
It looks like they should be TemplateStringsArray
but don't want to touch StaticQuery in this PR
I’ve currently set the default return for Not sure what the pattern is here with hooks yet but What do we think of this? @KyleAMathews @pieh @m-allanson @DSchau |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking awesome! Left some preliminary comments, and then I'll pull this down and validate later, as well!
@@ -1,4 +1,4 @@ | |||
import React from "react" | |||
import React, { useContext } from "react" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't a functional change necessarily, but maybe just use React.useContext
instead? We can check for the existence of that rather easily, e.g.
if (typeof React.useContext !== `function` && process.env.NODE_ENV === `development`) {
// warn
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also one note here... I think it's worth validating that these checks are stripped from our code in production (e.g. with our uglifier).
(not as a concern of this PR, speaking more broadly!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is tricky, because I would want this error to print out during built-html
stage (when we write to static html files), but not actually end up in client js bundle (just micro optim to save few bytes)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the check in gatsby-browser-entry.js
@pieh This will never end up in the bundle because I'll be stripping away the import and replacing it with static data
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All imports to useStaticQuery are now removed in build so this should be good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you are completely right - so this check would need to be done in babel plugin? (yuck - mixed concerns)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I guess it would need to be duplicated - in case there are 3rd party tools (Storybook etc) that want to use gatsby-browser-entry
outside of gatsby
"realm"?
packages/gatsby/index.d.ts
Outdated
@@ -20,4 +20,6 @@ export interface StaticQueryProps { | |||
|
|||
export class StaticQuery extends React.Component<StaticQueryProps> {} | |||
|
|||
export const useStaticQuery: (query: any) => void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sidharthachatterjee good call! I think I'd just do
export const useStaticQuery: (query: any) => null | object
packages/babel-plugin-remove-graphql-queries/src/__tests__/__snapshots__/index.js.snap
Outdated
Show resolved
Hide resolved
packages/babel-plugin-remove-graphql-queries/src/__tests__/index.js
Outdated
Show resolved
Hide resolved
Co-Authored-By: sidharthachatterjee <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's get the tests passing--otherwise I think this is good to go!
@vadistic We were doing some checks and yeah, generics are definitely way to go. Remaining question is default type for query results - it seems like using
this in practice forces users to either provide their type definition or use |
Current blocker:
|
@pieh good point, skipped my mind :) Query response will always be string indexed object extending scalar GraphQL types or arrays of those. We could provide index signatures for the first level but yeah, just go with As in my link above - |
) { | ||
report.panicOnBuild( | ||
`You're likely using a version of React that doesn't support Hooks. | ||
Please update to React 16.8.0 or later to use the useStaticQuery hook.` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you probably want to put in the same error message as above?
https://github.com/gatsbyjs/gatsby/pull/11588/files#diff-4bc662f92e2fed3fcf4fa02501ad6d3dR39
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch! Fixed.
Documentation for this is added in #11741 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
* master: (76 commits) chore(release): Publish Update relay version once again (gatsbyjs#11767) Update CHANGELOG.md (gatsbyjs#11764) chore(release): Publish Move to @gatsbyjs scoped version of yarn (gatsbyjs#11759) fix(blog): 2019-01-01 json code blocks (gatsbyjs#11750) fix(starters): update dependency gatsby to ^2.1.0 (gatsbyjs#11745) fix(starters): update dependency prop-types to ^15.7.2 (gatsbyjs#11748) feat(showcase): add Incremental.com.au (gatsbyjs#11729) feat(starters): add starter magicsoup.io (gatsbyjs#11670) docs(gatsby): Add documentation for useStaticQuery (gatsbyjs#11741) chore(release): Publish feat(gatsby): add useStaticQuery hook (gatsbyjs#11588) chore(release): Publish chore(docs): reword CSS in JS docs for clarity (gatsbyjs#11439) chore: Upgrade Prettier related packages to the latest (gatsbyjs#11735) fix(core): added event source polyfill in develop to fix IE/edge hmr (gatsbyjs#11582) chore: minify svg husky hook (gatsbyjs#10560) docs: add videos for Gatsby Link + rewrite for flow (gatsbyjs#11700) docs: add egghead lesson to quickstart (gatsbyjs#11699) ...
Adds a new React Hook called
useStaticQuery
Known Limitations
Todo
useContext
is undefinedThis is good to go now! Documentation and support for multiple instances will be added in later.