【发布时间】:2022-01-07 19:29:03
【问题描述】:
在重写客户端应用程序时遇到有效的钩子错误...(升级代码)
mobx 6.3.8 mobx react 7.2.1 和 mobx-state-tree 5.0.5 反应 17.0.1 RN 0.64.3
我觉得错误就在这里。我用谷歌搜索了使用商店的代码行,它把我带到了已弃用的网站……我不知道在https://mobx.js.org/react-integration.html 网站上哪里可以找到新的处理方法……这叫什么?
import { createContext, useContext } from "react"
import { RootStore } from "./root-store"
const RootStoreContext = createContext<RootStore>({} as RootStore)
const RootStoreProvider = RootStoreContext.Provider
// hook error here? // export const useStores = () => useContext(RootStoreContext);
错误: 错误:无效的挂钩调用。钩子只能在函数组件的主体内部调用。这可能是由于以下原因之一:
- 您可能有不匹配的 React 版本和渲染器(例如 React DOM)
- 您可能违反了 Hooks 规则
- 您可能在同一个应用程序中拥有多个 React 副本 有关如何调试和修复此问题的提示,请参阅 https://reactjs.org/link/invalid-hook-call。
添加更多上下文...根存储文件
import { Instance, SnapshotOut, types } from "mobx-state-tree"
import { creatMediaPlayerModel } from "../../models/media-player"
import { createUserModel } from "../../models/user"
import { createContentModel } from "../../models/content"
export const RootStoreModel = types.model("RootStore", {
mediaPlayerStore: creatMediaPlayerModel(),
userStore: createUserModel(),
contentStore: createContentModel(),
})
export type RootStore = Instance<typeof RootStoreModel>
export type RootStoreSnapshot = SnapshotOut<typeof RootStoreModel>
【问题讨论】:
-
这个错误究竟是什么意思?
-
添加到主帖
-
如果我注释掉 app.tsx rootstoreprovider 钩子错误消失
-
尝试将自定义钩子声明为函数声明
标签: reactjs typescript react-hooks mobx mobx-react