Gatsby: How to pass a variable from gatsby-node.js to pages

Created on 19 May 2019  ·  1Comment  ·  Source: gatsbyjs/gatsby

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.

question or discussion

Most helpful comment

@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

>All comments

@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

Was this page helpful?
0 / 5 - 0 ratings