document.getElementById("root");
Benefits
Drawbacks
React Workshop
Starting in 4 minutes
Topics
Benefits
Drawbacks
Not everything is interactive, most can be server rendered
* React components are run on the server
* HTML output is sent directly as the source
* string representation of the Virtual DOM
if there are client components, this describes where to insert at runtime
Works similarly to handle updates
But console errors are thrown if there is a mismatch
move data fetching to the server
smaller bundle size
can refetch without getting rid of client side state
but... you're probably going to need a framework to see the benefits
App Router - server components by default
opt in to client components with "use client"
Reconciliation still happens to diff the DOM created from static HTML with the virtual DOM
When a route is loaded with Next.js, the initial HTML is rendered on the server. This HTML is then progressively enhanced in the browser, allowing the client to take over the application and add interactivity, by asynchronously loading the Next.js and React client-side runtime.
Show the users the content that is ready
load interactive or data-reliant content in the background
place it above the imports
for example, if Menu imports MenuButton, MenuSlider, etc, it is assumed that these are also client components
"use client"
directive must be defined at the top of a file before any imports."use client"
does not need to be defined in every file. The Client module boundary only needs to be defined once, at the "entry point", for all modules imported into it to be considered a Client Component.export async function loadData() {
const res = await fetch('https://mh.com/pets', {
headers: {
authorization: process.env.API_KEY,
},
})
return res.json()
}
What's the issue?
import "server-only";
export async function loadData() {
const res = await fetch('https://mh.com/pets', {
headers: {
authorization: process.env.API_KEY,
},
})
return res.json()
};
Error!
import "client-only";
function interactWithUserInWeirdAntiquatedWay() {
window.prompt("How are you? ");
};
Using directly in a Server component might not work
Can wrap in a client component to pass to server component
'use client'
import { MagicList } from 'magic-list';
export default List;
const App = function() {
return (
<Provider data={data}>
<Layout />
</Provider>
)
}
useEffect
GraphQL - useQuery, useMutation, useSubscription