3.5及世博会第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状态树库进行状态处理和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 }从"react“导入{ RootStore },从"./root- STORE”导入用户存储
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>有什么想法吗?小费?整个登录方法都是错误的吗?调试器显示为loginuser未定义。

发布于 2021-12-10 18:09:39
我还补充了我的评论,作为一个回答:
中使用上下文
useContext(RootStoreContext)您所描述的行为是上下文提供程序从未设置时所发生的情况。
因此,您需要确保设置了提供程序并给出了值:
<RootStoreContext.Provider value={{ ... }}>
<MyComponent />
</RootStoreContext.Provider>需要访问该上下文的组件是该提供程序的子组件。
https://stackoverflow.com/questions/70295378
复制相似问题