Это моя часть моего кода на данный момент
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_ или любым другим способом передать значение вниз.
@ 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 для вставки данных в отдельные компоненты? или этот сингл на самом деле является шаблоном? И, вероятно, вам нужно что-то вроде этого:И на
single.js
:Не стесняйтесь оставлять отзывы