Skip to content
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

How to pass a variable from gatsby-node.js to pages #14152

Closed
Hypothesis-github opened this issue May 19, 2019 · 1 comment
Closed

How to pass a variable from gatsby-node.js to pages #14152

Hypothesis-github opened this issue May 19, 2019 · 1 comment
Labels
type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@Hypothesis-github
Copy link

This is my part of my code at the moment

        const posts = result.data.allCockpitHello.edges
        const date = new Intl.DateTimeFormat("default", {
          month: "short",
          day: "2-digit",
          year: "numeric",
        }).format(posts.node.cockpitCreated,);
        posts.forEach((post, index) => {
          const previous = index === posts.length - 1 ? null : posts[index + 1].node
          const next = index === 0 ? null : posts[index - 1].node

          createPage({
            path: `/blog/${post.node.Name.value}`,
            component: path.resolve(`./src/components/single.js`),
            context: {
              slug: post.node.Name.value,
              date,
              previous,
              next,
            },
          })
        })

I was wondering how I can pass date as prop to single.js or any other way I can pass the value down.

@gatsbot gatsbot bot added the type: question or discussion Issue discussing or asking a question about Gatsby label May 19, 2019
@jonniebigodes
Copy link

jonniebigodes commented May 19, 2019

@Hypothesis-github before all, are you using the createPage api to inject data into single components? or that single is actually a template? And what probably you want is something like this:

const posts = result.data.allCockpitHello.edges
        const date = new Intl.DateTimeFormat("default", {
          month: "short",
          day: "2-digit",
          year: "numeric",
        }).format(posts.node.cockpitCreated,);
        posts.forEach((post, index) => {
          const previous = index === posts.length - 1 ? null : posts[index + 1].node
          const next = index === 0 ? null : posts[index - 1].node

          createPage({
            path: `/blog/${post.node.Name.value}`,
            component: path.resolve(`./src/components/single.js`),
            context: {
              slug: post.node.Name.value,
              cockpidate:date,
              previousitem:previous,
              nextitem:next,
            },
          })
        })

And on single.js:

import React from "React"
import {Link} from "gatsby"

const Single=props=>{
   const {pageContext}= props
   const {slug,cockpidate,nextitem,previousitem}= pageContext
  return (
      <div>
        <h3>{slug}</h3>
         <hr/>
         <h5>created at {cockpidate}</h5>
         <Link to={nextitem}/>next item</Link>
          <Link to={previousitem}/>previousitem</Link>
       <div>
  )
}
export default Single

Feel free to provide feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

2 participants