使用 useContext 传递数据,不使用 Mobx 共享
parent
6e522a547c
commit
69dfeaeab7
@ -1,24 +1,12 @@
|
|||||||
import { makeAutoObservable, runInAction } from 'mobx'
|
|
||||||
|
|
||||||
const KEY_USER_ID = 'KEY_USER_ID'
|
const KEY_USER_ID = 'KEY_USER_ID'
|
||||||
|
|
||||||
class Auth {
|
class Auth {
|
||||||
|
|
||||||
constructor(root) {
|
constructor() {
|
||||||
makeAutoObservable(this, { rootStore: false })
|
|
||||||
this.root = root
|
|
||||||
this.userId = root.getSession(KEY_USER_ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
setup(userId) {
|
|
||||||
runInAction(() => {
|
|
||||||
this.userId = userId
|
|
||||||
})
|
|
||||||
this.root.putSession(KEY_USER_ID, userId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// HT 账号 OP_SN
|
// HT 账号 OP_SN
|
||||||
userId = null
|
userId = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Auth
|
export default Auth
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
import { createContext, useContext } from 'react'
|
||||||
|
|
||||||
|
export const AuthContext = createContext(null)
|
||||||
|
|
||||||
|
export function useAuth() {
|
||||||
|
return useContext(AuthContext)
|
||||||
|
}
|
Loading…
Reference in New Issue