Gatsby 노트 5 (Contentful-1)
참고
학습 내용
Contentful

절차

The example project가 생성된다 (여기서는 사용하지 않는다)
절차 (새 프로젝트 만들기)


데이터 구조 만들기
1.Content modal => Add content type

2.Add field

데이터 생성

Field ID는 GraphQL로 데이터 취득시에 이용한다



GraphQL로 contentful 데이터 얻기
yarn add gatsby-source-contentful
gatsby-config.js
const dotenv = require("dotenv")
if (process.env.ENVIRONMENT !== "production") {
dotenv.config()
}
:
:
//(省略)
{
resolve: `gatsby-source-contentful`,
options: {
spaceId: process.env.CONTENTFUL_SPACE_ID,
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN,
},
},

src/pages/index.js
import React from "react"
import { graphql, useStaticQuery } from "gatsby"
import Layout from "../components/layout"
import Kv from "../components/kv"
import BlogItem from "../components/blogItem"
import {Container, Row, Col} from 'react-bootstrap'
const IndexPage = () => {
// contentfulからデータを取得
const data = useStaticQuery(graphql`
query {
allContentfulBlogPost(sort: {
fields: createdDate,
order: ASC
}) {
edges {
node {
title
slug
createdDate(formatString: "YYYY/MM/DD")
thumbnail {
fluid {
src
}
}
}
}
}
}
`)
return (
<Layout>
<Kv />
<Container>
<Row>
{
data.allContentfulBlogPost.edges.map((edge, index) => (
<Col sm={4} key={index}>
<BlogItem
title={edge.node.title}
date={edge.node.createDate}
src={edge.node.thumbnail.fluid.src}
link={`blog/${edge.node.slug}`} />
</Col>
))
}
</Row>
</Container>
</Layout>
)
}
export default IndexPage
Reference
이 문제에 관하여(Gatsby 노트 5 (Contentful-1)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/hththt/items/bfcb2943e5f717a1a75f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)