useStaticQuery outside a React component #29300
Unanswered
benweissmann
asked this question in
Help
Replies: 1 comment 1 reply
-
Because it uses React's context to distribute the data. Each query gets hashed and they can then access the same data from any react component. It's feasible that this could be achieved without react and just injecting the json as a variable in that file, but there's some complications with that, and it also leads to duplication. Why can't you make a function that accepts an argument (the data) and then a react component that uses |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to write a helper function that will be used both inside React components, as well as outside React components. This helper needs to access some data that can be fetched at build-time.
I'm struggling to understand why there isn't an equivalent to
useStaticQuery
that can be used outside the context of a React component. Reading through the documentation on howuseStaticQuery
is implemented, it seems like the Gatsby build system parses the file, extracts the query, runs the query and saves the result as a JSON file, and then replaces the call touseStaticQuery
with a call to, roughly,require('path/to/staticquery/result.json')
which then gets bundled by Webpack.Why does this have to be done inside a React component? Why can't non-React javascript files also use static queries to load data at build time?
Beta Was this translation helpful? Give feedback.
All reactions