---
title: "从完整消息和你自己的代码行入手修复 Minified React error #418 和 #185"
description: "Minified React error 是一个编号，代表一条更长的消息。打开它输出的 react.dev/errors 链接即可读到全文。我们在 Next.js 16 中触发了 #418 和 #185，并修复了这两个错误。"
canonical: "https://logbrew.co/zh-Hans/errors/minified-react-error"
last_updated: "2026-11-20"
---

# 从完整消息和你自己的代码行入手修复 Minified React error #418 和 #185

Minified React error 是一个编号，代表一条更长的消息。打开它输出的 `react.dev/errors` 链接即可读到全文。我们在 Next.js 16 中触发了 #418 和 #185，并修复了这两个错误。

2026年11月20日 用 Next.js 16.4.6、React 19.2.7、Chrome 157和@logbrew/next 0.1.18 实测

作者 [Furkan Erday](https://github.com/furkanerday). Furkan Erday 是 LogBrew 的创始人，每天用 Claude Code 和 Codex 开发 LogBrew。

Canonical: https://logbrew.co/zh-Hans/errors/minified-react-error

## error boundary 捕获了哪些 React 错误

| 错误抛出的位置 | boundary 已捕获 | 浏览器中有真实消息 |
| --- | --- | --- |
| 客户端组件的渲染 | 是 | 是 |
| `useEffect` | 是 | 是 |
| `onClick` 处理函数 | 否 | 是 |
| `setTimeout` 回调 | 否 | 是 |
| async 处理函数中 `await` 之后 | 否 | 是 |
| Server Component | 是 | 否，`#441` 和一个 digest |

在 1 个基于 React 19.2.7 的 Next.js 16.4.6 生产构建中制造的 6 个错误。

## 先读完整消息，再找到那一行

### 打开消息里的链接

生产环境的 React 不把消息正文打进 bundle，而是输出一个编号和一个链接。打开这个链接。`react.dev/errors` 页面会显示全文，并把 URL 中每个 `args[]` 的值填进去。`next dev` 使用的开发构建会直接输出全文。

```bash
"use client";
export function RenderedAt() {
  return <p>Rendered at {Date.now()}</p>;
}
```

输出

```text
Error: Minified React error #418; visit https://react.dev/errors/418?args[]=text&args[]= for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
```

### #418 表示服务端 HTML 与首次渲染不一致

解码器把 #418 解读为 `Hydration failed because the server rendered text didn't match the client`。`args[]=text` 表示某个文本节点不同，就像上面 `Date.now()` 的例子。`args[]=HTML` 表示元素不同。把 `<div>` 放进 `<p>` 时我们得到的是 `HTML`，因为浏览器会在 `<div>` 之前关闭 `<p>`。两次运行中，React 都渲染了浏览器端的版本，页面继续正常工作。

```bash
export default function Page() { return <p>Order total <div>12 USD</div></p>; }
```

输出

```text
Error: Minified React error #418; visit https://react.dev/errors/418?args[]=HTML&args[]= for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
```

### React 19.2 上没有出现 #423

解码器把 #423 解读为 `There was an error while hydrating but React was able to recover by instead client rendering the entire root.` 我们在 React 19.2.7 上制造了 3 种不一致，其中 1 种在 `<Suspense>` 里，React 每次都输出 #418，从未输出 #423。如果你的日志里有 #423，就按 #418 的原因去排查。

### #185 来自每次渲染后都设置 state 的 layout effect

解码器把 #185 解读为 `Maximum update depth exceeded`。我们没有依赖数组的 `useLayoutEffect` 在每次渲染后都设置 state，React 用 #185 中止了循环，Next.js 把页面换成了 `This page couldn’t load`。同样的代码放在 `useEffect` 里则完全没有报错。2 次运行中，我们读取页面时计数器分别显示 228,936 和 231,224。

```bash
"use client";
import { useLayoutEffect, useState } from "react";

export default function Page() {
  const [count, setCount] = useState(0);
  useLayoutEffect(() => {
    setCount(count + 1);
  });
  return <p>{count}</p>;
}
```

输出

```text
Error: Minified React error #185; visit https://react.dev/errors/185 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
```

### 用 source map 把堆栈映射回你的文件

在 `next.config` 中设置 `productionBrowserSourceMaps: true` 并重新构建。Turbopack 会给每个 map 一个独立的哈希文件名，所以 `resolve.mjs` 先从 chunk 的最后一行读出这个名字，再用 Node 内置的 `SourceMap` 类查找位置。我们把 #185 堆栈中唯一一个不在 React 里的帧 `2-5_qr0vv7do_.js:1:254` 交给它，它指向了我们页面第 7 行的 `setCount`。

```bash
// node resolve.mjs <chunk.js> <line> <column>
import { readFileSync } from "node:fs";
import { SourceMap } from "node:module";
import { dirname, join } from "node:path";

const [chunk, line, column] = process.argv.slice(2);
const mapName = readFileSync(chunk, "utf8").match(/sourceMappingURL=(\S+)\s*$/)[1];
const map = new SourceMap(JSON.parse(readFileSync(join(dirname(chunk), mapName), "utf8")));
const { originalSource, originalLine, originalColumn, name } = map.findEntry(line - 1, column - 1);
console.log(`${name ?? "?"} ${originalSource}:${originalLine + 1}:${originalColumn + 1}`);
```

输出

```text
setCount turbopack:///[project]/app/loop-layout-effect/page.tsx:7:5
```

## 修复原因，再捕获剩下的错误

### 修复 #418，让服务端和首次加载渲染相同的输出

在 `useEffect` 中读取只有浏览器才有的值，比如 `Date.now()`、`window` 或 `localStorage`。它只在浏览器中、水合之后运行，所以首次渲染与服务端 HTML 一致，我们这次运行中 #418 消失了。对于放在 `<p>` 里的 `<div>`，把 2 个标签中的 1 个换掉。把 `<p>` 里的元素换成 `<span>` 后就没有报错了。

```bash
"use client";
import { useEffect, useState } from "react";

export function RenderedAt() {
  const [renderedAt, setRenderedAt] = useState<number | null>(null);
  useEffect(() => setRenderedAt(Date.now()), []);
  return <p>Rendered at {renderedAt ?? "…"}</p>;
}
```

### `suppressHydrationWarning` 只用于 1 个无法对齐的值

`suppressHydrationWarning` 在我们这次运行中阻止了 #418，但页面保留了服务端的文本。它显示的是构建时的时间，而不是浏览器里的时间。它只对 1 层生效，React 称它为应急出口。

```bash
"use client";
export function RenderedAt() {
  return <p suppressHydrationWarning>Rendered at {Date.now()}</p>;
}
```

### 修复 #185，给 effect 加上依赖数组

使用 `[]` 后，layout effect 只运行了一次，页面显示 1，没有报错。把 effect 读取的值列进数组，只在其中某个值变化时才设置 state。

```bash
"use client";
import { useLayoutEffect, useState } from "react";

export default function Page() {
  const [count, setCount] = useState(0);
  useLayoutEffect(() => {
    setCount(count + 1);
  }, []);
  return <p>{count}</p>;
}
```

### 用 error boundary 捕获渲染错误

error boundary 是一个类组件。`getDerivedStateFromError` 让它切换到备用界面，`componentDidCatch` 负责上报错误。我们的 boundary 捕获了渲染时 `taps.map` 抛出的 `TypeError`，以及 `useEffect` 中抛出的错误。它漏掉了在 `onClick` 处理函数、`setTimeout` 回调以及 async 处理函数中 `await` 之后抛出的错误。这些错误以未捕获错误的形式到达 window，所以要在抛出的地方就地捕获。

```bash
"use client";
import { Component, type ReactNode } from "react";

export class ErrorBoundary extends Component<{ children: ReactNode }, { error: Error | null }> {
  state: { error: Error | null } = { error: null };

  static getDerivedStateFromError(error: Error) {
    return { error };
  }

  componentDidCatch(error: Error) {
    console.error("ErrorBoundary caught:", error.message);
  }

  render() {
    if (this.state.error) return <p>This part of the page failed to load.</p>;
    return this.props.children;
  }
}
```

输出

```text
ErrorBoundary caught: Cannot read properties of undefined (reading 'map')
```

### 在 Next.js 中添加 `error.tsx`，并把错误发送到 LogBrew

Next.js 会用一个渲染 `error.tsx` 的 error boundary 包裹每个路由段。我们的 `error.tsx` 捕获了同一个渲染错误，`@logbrew/next` 中的 `captureNextClientError()` 把它作为 1 个问题发送出去。演示中传入了一个记录用的 transport。去掉 `transport`，客户端就会用 `fetch` 发送到 LogBrew。

```bash
// app/next-error.tsx
"use client";
import { useEffect } from "react";
import { captureNextClientError, createLogBrewNextBrowserClient } from "@logbrew/next/client";
import { RecordingTransport } from "@logbrew/sdk";

const transport = RecordingTransport.alwaysAccept();
const logbrew = createLogBrewNextBrowserClient({
  clientKey: process.env.NEXT_PUBLIC_LOGBREW_CLIENT_KEY,
  transport
});

export function ReportError({ error, route }: { error: Error & { digest?: string }; route: string }) {
  useEffect(() => {
    void captureNextClientError(logbrew, error, { routeTemplate: route }).then((response) => {
      console.log(JSON.stringify(response));
      console.log(transport.lastBody());
    });
  }, [error, route]);
  return <p>This page failed to load.</p>;
}

// app/next-boundary/error.tsx
"use client";
import { ReportError } from "../next-error";

export default function Error({ error }: { error: Error & { digest?: string } }) {
  return <ReportError error={error} route="/next-boundary" />;
}
```

输出

```text
{"statusCode":202,"attempts":1,"batches":1}
```

## 我们实测看到的结果

我们用 Turbopack 通过 `next build` 构建了 1 个基于 React 19.2.7 的 Next.js 16.4.6 应用，用 `next start` 启动，并在无头 Chrome 157 中打开每个页面。我们制造了 3 种水合不一致、2 种更新循环，以及发生在不同位置的 6 个错误，然后在同一个构建上逐一验证修复。

每种水合不一致都输出了 #418，从未输出 #423。只有 layout effect 的循环输出了 #185。error boundary 捕获了 6 个错误中的 3 个，浏览器在其中 5 个错误上看到了真实消息。

Server Component 的错误比较特殊。页面返回 500，浏览器收到的不是我们的消息，而是带有 `digest` `1331393307` 的 `Minified React error #441`。服务端日志用同一个 digest 打印了 `Orders database is down`，所以 digest 能把你带到真正的错误。

输出

```text
⨯ Error: Orders database is down
    at b (.next/server/chunks/ssr/[root-of-the-server]__0e6swfn4zoar-._.js:1:1281) {
  digest: '1331393307'
}
```

## 为什么生产环境的 React 输出编号而不是消息

React 的生产构建只保留每个错误的编码，以减少传输的字节数。正文存放在 React 仓库的 `scripts/error-codes/codes.json` 中，解码器页面再从 URL 填入参数。#441 不一样。React 在生产构建中有意去掉 Server Component 的消息，让服务端的细节永远不会到达浏览器。

堆栈同样经过压缩。对于 #418，source map 帮助不大。我们 #418 堆栈中的每一帧都指向 `react-dom-client.production.js`，没有一帧指向我们的代码。`args[]` 的值和你最近改动的组件能更快带你找到原因。

vercel/next.js#50245 说明了解码后的正文为什么重要。这个 issue 在 2023 年 5 月报告了只在生产环境出现的 Minified React error。一位维护者写道，帖子里的人很可能是在同一条消息背后遇到了不同的问题，这个 issue 在 2024 年 8 月因没有复现而关闭。解码后的消息和源码行能把这些情况区分开来。

## LogBrew 记录了什么

`captureNextClientError()` 发送了 1 个标题为 `TypeError`、级别为 `error` 的问题，带有消息、机制 `nextjs.error_boundary` 和 `handled: false`。它保留了 10 个堆栈帧，每帧都有文件、行、列和函数。第一帧是 `19k2sxe-2yvvy.js` 第 1 行第 220 列，我们的 source map 把它映射到了页面第 5 行的 `taps.map`。

LogBrew 按错误类型和这第一帧把重复错误聚合在一起，键是 `nextjs.error_boundary:TypeError:` 后接 chunk 的 URL。问题保留了消息正文，而它原因链中的条目把消息标记为 `redacted`。

对于 Server Component 的错误，问题带有 #441 的消息和 `errorDigest` `1331393307`，与服务端日志中的值相同。原因链记录为 `stackFramesState: "not_captured"`，因为浏览器从未拿到服务端的堆栈。

## 常见问题

### 什么是 Minified React error？

来自 React 生产构建的错误。React 把消息换成一个编号和指向 `react.dev/errors` 的链接，那个页面会显示全文。开发构建会直接输出全文。

### Minified React error #418 是什么意思？

表示水合失败。服务端返回的 HTML 与浏览器中的首次渲染不一致。`args[]=text` 指向文本节点，`args[]=HTML` 指向元素。`Date.now()`、`typeof window` 判断、按用户区域设置显示的日期，以及放在 `<p>` 里的 `<div>` 都会导致它。

### 如何修复 Next.js 布局中的水合错误？

和任何组件一样，找出这个布局在服务端和浏览器之间有什么不同。在根布局中，在 React 加载前修改 `<html>` 或 `<body>` 的主题脚本或浏览器扩展也会导致它。在该元素上加 `suppressHydrationWarning` 可以在 1 层内隐藏这个错误。

### Minified React error #185 是什么意思？

表示超出了最大更新深度。React 应用更新时，某个组件一次又一次地设置 state。在我们的运行中，原因是没有依赖数组的 `useLayoutEffect`。加上依赖数组，或者只在值变化时设置 state。

### error boundary 捕获不到哪些错误？

在我们的运行中，它漏掉了 `onClick` 处理函数、`setTimeout` 回调以及 `await` 之后抛出的错误。它捕获了渲染、`useEffect` 和 Server Component 中的错误，但最后一种到达时是不带消息的 #441。

### 为什么我在 ChatGPT 或其他网站上看到 Minified React error？

该网站运行的是 React 的生产构建，它的代码遇到了错误。只有网站的开发者能修复这段代码。在 React 加载前修改页面的浏览器扩展也可能导致 #418，可以在关闭扩展的无痕窗口中试试。

## 捕获生产环境中的每个 React 错误

LogBrew Next.js SDK 会把每个到达 error.tsx 的错误连同堆栈帧和服务端 digest 一起发送，LogBrew 把重复出现的错误聚合为 1 个问题。免费额度充足，无需绑卡。

[免费开始](https://logbrew.co/zh-Hans)

## 参考来源

- [React docs, Error decoder](https://react.dev/errors)
- [React docs, Catching rendering errors with an error boundary](https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary)
- [React docs, Common props (suppressHydrationWarning)](https://react.dev/reference/react-dom/components/common#common-props)
- [Next.js docs, Text content does not match server-rendered HTML](https://nextjs.org/docs/messages/react-hydration-error)
- [Next.js docs, error.js](https://nextjs.org/docs/app/api-reference/file-conventions/error)
- [Next.js docs, productionBrowserSourceMaps](https://nextjs.org/docs/app/api-reference/config/next-config-js/productionBrowserSourceMaps)
- [vercel/next.js#50245](https://github.com/vercel/next.js/issues/50245)

## 更多错误修复

- [修复隐藏了消息、文件和行号的脚本错误](https://logbrew.co/zh-Hans/errors/script-error)
- [修复 Failed to fetch 错误，分清它的 3 种原因](https://logbrew.co/zh-Hans/errors/failed-to-fetch)
- [从消息入手修复 Kotlin 和 Android 中的 java.lang.NullPointerException](https://logbrew.co/zh-Hans/errors/nullpointerexception)
- [我们复现过的错误，以及每个错误的修复方法](https://logbrew.co/zh-Hans/errors)
