45
loading...
This website collects cookies to deliver better user experience
Using Axios and fetching the data on the client
Using the prebuilt function GetServerSideProps and rendering on the server
export interface Post {
identifier: string
title: string
body?: string
slug: string
subName: string
username: string
createdAt: string
updatedAt: string
sub?: Sub
mediaLink?: string
bodyPreview?: string
url: string
voteScore?: number
commentCount?: number
userVote?: number
}
const [posts, setPosts] = useState<Post[]>([])
useEffect(() => {Axios.get('/posts').then((res)=>setPosts(res.data)).catch((err) => console.log(err))}, [])
{posts?.map((post)=>(<PostPreview post={post} key={post.identifier}/>))}
<img src={mediaLink}/>
import { GetServerSideProps } from 'next'
export const getServerSideProps: GetServerSideProps = async (context) => {try {
const res = await Axios.get('/post')
return { props: { posts: res.data } }
} catch (err) {
return { props: { error: 'Something went wrong' }}
}}
<Image src={mediaLink} width={16} height={16} layout="responsive"/>