patrick
patrick

Reputation: 353

Create variables without the createPages API in Gatsby?

I was wondering if I could use variables that I create with the createPages API outside the component template.

My scenario is that I create a variable like this:

            pages.forEach(page => {
                createPage({
                    path: `${page.slug}`,
                    component: path.resolve(`src/templates/page-template.js`),
                    context: {
                        title: page.title // My Variable
                    },
                })

Now, in my page-template.js file I import a component that holds an external graphql query that is not included in the page-template.js file but I want to use variables in the query.

Can I create variables without the createPages API in Gatsby?

Upvotes: 2

Views: 88

Answers (1)

coreyward
coreyward

Reputation: 80041

You can access this via the pageContext prop. E.g:

const YourComponent = ({ pageContext }) => 
  <SomeOtherComponent title={pageContext.title} />

This assumes that SomeOtherComponent is running this GraphQL query against an external DB, not the local Gatsby GraphQL server, though.

Upvotes: 1

Related Questions