来自用户存储的未定义操作调用

undefined action call from userstore

3.5 和 Expo 版本 43

我们正在移动一个现有的代码文件,以便在我们的用户存储模型中使用... 很难找到 .actions 项,也不知道如何研究它。

我们尝试在其中使用操作的示例代码

 const { userStore } = useStores()


 const onPressLogIn = async () => {
debugger
console.log("pressed login")
console.log(userStore)  //also undefined
if (validateText()) {
  setLoading(true)
  props.navigation.setParams({ noback: true })
  userStore.logInUser(email, password)
  // .then(async () => {
    //   return logInStatus
    // })
    // .catch(() => {
    //   setLoading(false)
    //   props.navigation.setParams({ noback: false })
    //   setError(userStore.friendlyLoginStatus)
    // })
}
}

使用 mobx-state-tree 库进行状态处理和 firebase Auth

async logInUser(email: string, password: string) {
      const auth = getAuth()
      signInWithEmailAndPassword(auth, email, password).then((userCredential) => {
        // Signed in 
        const user = userCredential.user;
        // ...
      })
      // await self.loginSuccess(data.user, "email")
      // return Promise.resolve()

  },

**我们从哪里导入用户商店 *** 从“反应”导入{createContext,useContext} 从“./root-store”导入{RootStore}

const RootStoreContext = createContext<RootStore>({} as RootStore)


export const RootStoreProvider = RootStoreContext.Provider

export const useStores = () => useContext(RootStoreContext)

*** 根存储文件

import { Instance, SnapshotOut, types } from "mobx-state-tree"

import { creatMediaPlayerModel } from "../../models/media-player"
import { createUserModel } from "../../models/user"
import { createContentModel } from "../../models/content"

// /**
//  * A RootStore model.
//  */
export const RootStoreModel = types.model("RootStore").props({
mediaPlayerStore: creatMediaPlayerModel(),
userStore: createUserModel(),
contentStore: createContentModel(),
})

// /**
//  * The RootStore instance.
//  */
export type RootStore = Instance<typeof RootStoreModel>

// /**
//  * The data of a RootStore.
//  */
export type RootStoreSnapshot = SnapshotOut<typeof RootStoreModel>

有什么想法吗?尖端?整个登录方法写错了吗?调试器显示为登录用户未定义

添加我的评论作为答案:

您正在使用

中的上下文
useContext(RootStoreContext)

并且您描述的行为是从未设置上下文提供程序时发生的情况。

因此您需要确保提供程序已设置并被赋予值:

<RootStoreContext.Provider value={{ ... }}>
  <MyComponent />
</RootStoreContext.Provider>

并且需要访问该上下文的组件是该提供程序的子项。