vercel / vercel/next.js

private fields broken in specific case

Open
#70,834 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Runtime
Dominant language
JavaScript
Stars
142k
Forks
32.4k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/cool-yalow-zzkp58

To Reproduce

Click preview
Wait for it to hydrate
It throws an error: "attempted to get private field on non-instance"

Current vs. Expected behavior

Here is the same code running in react without Next.js:
https://codesandbox.io/p/sandbox/tslrlg
The getter does not throw and returns the value of the private field.

In the broken Next.js version, it renders fine on the server side, but when it rehydrates it throws.
The bug only happens if all of these are true:

  • using Next.js
  • a function is imported from another file
  • that looks like this: () => new class ClassName {/* class body */}
  • the returned instance is passed through useMemo or useRef

The following function formats do not trigger the error:

  • () => { return new class ClassName {/* class body */} }
  • () => new ClassName()
  • () => instance
  • exporting the class constructor
"use client";
import { useRef } from "react";
import { construct } from "./construct";

export default function Home() {
  const a = construct()
  const ref = useRef(a);
  const b = ref.current;

  return b.test;      // throws client-side
}
export const construct = () => new class {
  #test = 99
  get test() { return this.#test }
}
Provide environment information
It's running on CodeSandbox
Which area(s) are affected? (Select all that apply)

Runtime

Which stage(s) are affected? (Select all that apply)

next dev (local)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the CodeSandbox reproducer, comparing the Next.js and React-only examples, then trace the Home component’s useRef path and imported construct function during client hydration. Done means the Next.js example hydrates without the private-field error and b.test returns 99; no repository file or test is named in the report.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nextjs, react
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.