Created
March 1, 2021 03:24
-
-
Save kuc-arc-f/4a65043d21b7219824f9b7859b42c861 to your computer and use it in GitHub Desktop.
Gatsby.js API Fetch + HeadlessCMS, sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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