Skip to content

Instantly share code, notes, and snippets.

@guydumais
Created August 15, 2021 11:25
Show Gist options
  • Save guydumais/3f99741e5e522affa2aa0896ff7aa1ae to your computer and use it in GitHub Desktop.
Save guydumais/3f99741e5e522affa2aa0896ff7aa1ae to your computer and use it in GitHub Desktop.
Next.js Client Side Rendering (CSR)
// React
import { useEffect, useState } from 'react'
// Next.js
import Head from 'next/head'
import Link from 'next/link'
// Custom Components
import BackToHome from 'components/BackToHome'
// Page component
export default function ClientSideRendered() {
const [state, setState] = useState([] as any)
const getData = async () => {
const res = await fetch('https://reqres.in/api/users?page=2')
const jsonData = await res.json()
setState(jsonData)
}
useEffect(() => {
getData()
}, [])
return (
<>
<Head>
<title>Client-Side Rendering (CSR) • Guy Dumais</title>
<meta name="description" content="Example page using Client-Side Rendering (CSR) with Next.js 11 and React 17"/>
<meta name="viewport" content="initial-scale=1.0, width=device-width" />
</Head>
<BackToHome/>
<h1>Client-Side Rendering (CSR)</h1>
<p>Data fetched on the client-side only.</p>
<ul>
{
state.data?.map((e) => (
<li key={e.id}>{e.email}</li>
))
}
</ul>
</>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment