Gatsby: Как передать переменную из gatsby-node.js на страницы

Созданный на 19 мая 2019  ·  1Комментарий  ·  Источник: gatsbyjs/gatsby

Это моя часть моего кода на данный момент

        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,
            },
          })
        })

Мне было интересно, как я могу передать date качестве опоры в _single.js_ или любым другим способом передать значение вниз.

question or discussion

Самый полезный комментарий

@ Hypothesis-github, прежде всего, вы используете createPage api для вставки данных в отдельные компоненты? или этот сингл на самом деле является шаблоном? И, вероятно, вам нужно что-то вроде этого:

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,
            },
          })
        })

И на 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

Не стесняйтесь оставлять отзывы

>Все замечания

@ Hypothesis-github, прежде всего, вы используете createPage api для вставки данных в отдельные компоненты? или этот сингл на самом деле является шаблоном? И, вероятно, вам нужно что-то вроде этого:

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,
            },
          })
        })

И на 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

Не стесняйтесь оставлять отзывы

Была ли эта страница полезной?
0 / 5 - 0 рейтинги

Смежные вопросы

ghost picture ghost  ·  3Комментарии

3CordGuy picture 3CordGuy  ·  3Комментарии

totsteps picture totsteps  ·  3Комментарии

rossPatton picture rossPatton  ·  3Комментарии

KyleAMathews picture KyleAMathews  ·  3Комментарии