React Router 6 问题:Invalid hook call. Hooks can only be called inside of the body of a function compo

张开发
2026/4/7 16:35:08 15 分钟阅读

分享文章

React Router 6 问题:Invalid hook call. Hooks can only be called inside of the body of a function compo
function showOutlet() { const result useOutlet(); console.log(result); }问题原因在 React 中使用 React Router 6 中的 useOutlet 函数出现如下错误信息Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app See https://react.dev/link/invalid-hook-call for tips about how to debug and fix this problem. at showOutlet (Message.tsx:32:24) at onClick (Message.tsx:67:36)在普通函数中调用了 Hook 函数 useOutlet这违反了 React Hook 的使用规则React Hooks 只能在以下地方调用React 函数组件内部、自定义 Hook 内部处理策略改为在组件中直接使用const result useOutlet(); function showOutlet() { console.log(result); }

更多文章