Skip to content

Instantly share code, notes, and snippets.

@kuc-arc-f
Created March 1, 2021 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuc-arc-f/4a65043d21b7219824f9b7859b42c861 to your computer and use it in GitHub Desktop.
Save kuc-arc-f/4a65043d21b7219824f9b7859b42c861 to your computer and use it in GitHub Desktop.
Gatsby.js API Fetch + HeadlessCMS, sample
// Gatsby.js API Fetch + HeadlessCMS, sample
import React, { useState, useEffect } from "react"
const IndexPage = () => {
const [postItems, setPostItems] = useState([])
const site_id = "123"
const base_url = "http://hoge.com"
var url = base_url+`/api/get/find?content=posts&site_id=${site_id}`
useEffect( ()=>{
fetch(url)
.then(response => response.json())
.then(resultData => {
// console.log( resultData )
setPostItems(resultData)
})
}, [])
return (
<section>
<h1>Gatsby - index5</h1>
<hr />
<p>
Build Time Data: Gatsby repo{` `}
</p>
<hr />
{postItems.map((item, index) => {
// console.log(item.id)
return (<div key={index}
id={item.id} title={item.title}><h3>{item.title}</h3>
ID : {item.id}
</div>
)
})}
</section>
)
}
export default IndexPage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment