Skip to content

Instantly share code, notes, and snippets.

@raflyfahrezi
Created May 26, 2021 10:34
Show Gist options
  • Save raflyfahrezi/b5462fbafc23f6809b8602fd2f230952 to your computer and use it in GitHub Desktop.
Save raflyfahrezi/b5462fbafc23f6809b8602fd2f230952 to your computer and use it in GitHub Desktop.
fetching data using getStaticProps and SWR
import useSWR from 'swr'
import axios from 'axios'
import React from 'react'
const index = ({ initialData }) => {
const { data } = useSWR('...some API path...', fetcher, {
initialData: initialData,
refreshInterval: 5000,
})
return (
<div>
<div>
{data.map((item, index) => {
return (
<div>...</div
)
})}
</div>
</div>
)
}
const getStaticProps = async () => {
const response = await axios.get('...some API Path...')
return {
props: { initialData: response.data },
revalidate: 1,
}
}
export default index
export { getStaticProps }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment