Our examples are located under nodejs/next:
- nodejs/next/hello-world: a hello world. There's an in-tree one at: github.com/vercel/next.js/tree/e75361fd03872b097e817634c049b3185f24cf56/examples/hello-world, but ours is truly minimal
- nodejs/next/hoc: shows how to use a higher order component (HOC) to factor out
getStaticProps
across two pages: nodejs/next/hoc/pages/index.js and nodejs/next/hoc/pages/notindex.js - nodejs/next/typescript: simple TypeScript example, minimized from: github.com/vercel/next.js/tree/d61b0761efae09bd9cb1201ff134ed8950d9deca/examples/with-typescriptNotably, that shows how
require
errors are avoided in that case as mentioned at: stackoverflow.com/questions/64926174/module-not-found-cant-resolve-fs-in-next-js-application/70363153#70363153 - nodejs/next/localStorage: a counter that is persistent across page reloads by using
localStorage
. Used in: stackoverflow.com/questions/54819721/next-js-access-localstorage-before-rendering-page/68136224#68136224
Solved ones:
- solved by preview mode in Next.js 12:
- ISR was basically unusable for CRUD websites because you can't force a one-off immediate page update:
The goal of this example is to understand when states and effects happen when changing between different routes that use the same component.
Behavior is follows:
- visit: localhost:3001/1
- click
count++
. This makescount: 1
- click "2" to visit localhost:3001/2
- outcome: count is still 1
This is likely because in React the state kept in the virtual DOM structure, and identical structure implies identical state. So when we change from post 1 to 2, we still have a
Post
object, and state is unchanged.Next if we click:then the count is back to 0. This is because we changed the
- "Index" to go to localhost:3000
- "1" to go to localhost:3001/1
Post
object in the DOM to Index
and back, which resets everything.This example also illustrates how to prevent this from happening with
useEffect
.Bibliography:
This is a minimal reproducible example for the terrible problem of external effects applying twice to refs for effects that are not idempotent and thus blowup if applied twice.
The issue is currently discussed at: react.dev/learn/synchronizing-with-effects#step-3-add-cleanup-if-needed (archive) which says "you need to cleanup the thing yourself". web.archive.org/web/20240720100401/https://react.dev/learn/synchronizing-with-effects#subscribing-to-events is also says that for the specific case of
addEventListener
.But that's annoying! Can't we just somehow tell if we applied twice or not to avoid having to implement a cleanup? What if a third party system does not provide a cleanup at all?
Is the correct solution to just just have a
useEffect
with empty dependency list? Seems to be good according to posts and to ESLint!Tried to do a React only reproduction at: react/ref-twice.html.
Bibliography:
Ciro Santilli's questions:
In this example we attempt to inject React elements into statically rendered HTML coming from the server, and properly hydrate them.
Questions by Ciro Santilli:
Articles by others on the same topic
There are currently no matching articles.