Skip to content

Instantly share code, notes, and snippets.

@kuc-arc-f
Created March 1, 2021 03:20
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/883466ef55f4d7ba815dabd060a6ca31 to your computer and use it in GitHub Desktop.
Save kuc-arc-f/883466ef55f4d7ba815dabd060a6ca31 to your computer and use it in GitHub Desktop.
Gatsby.js API Fetch, sample
// Gatsby.js API Fetch, sample
// https://www.gatsbyjs.com/docs/data-fetching/#retrieving-data-with-the-fetch-api
import React, { useState, useEffect } from "react"
// import { graphql, useStaticQuery } from "gatsby"
const IndexPage = () => {
// Client-side Runtime Data Fetching
const [starsCount, setStarsCount] = useState(0)
useEffect(() => {
// get data from GitHub api
fetch(`https://api.github.com/repos/gatsbyjs/gatsby`)
.then(response => response.json()) // parse JSON from request
.then(resultData => {
console.log( "stargazers_count=", resultData.stargazers_count )
setStarsCount(resultData.stargazers_count)
}) // set data for the number of stars
}, [])
return (
<section>
<h1>Gatsby - index</h1>
<hr />
<p>
Build Time Data: Gatsby repo{` `}
</p>
<p>Runtime Data: Star count for the Gatsby repo {starsCount}</p>
</section>
)
}
export default IndexPage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment