Created
May 26, 2021 10:34
-
-
Save raflyfahrezi/b5462fbafc23f6809b8602fd2f230952 to your computer and use it in GitHub Desktop.
fetching data using getStaticProps and SWR
This file contains hidden or 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
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