mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-22 11:26:37 +08:00
* feat: add theme berry * docs: add development notes * fix: fix blank page * chore: update implementation * fix: fix package.json * chore: update ui copy --------- Co-authored-by: JustSong <songquanpeng@foxmail.com>
30 lines
824 B
JavaScript
30 lines
824 B
JavaScript
// contexts/User/index.jsx
|
|
import React, { useEffect, useCallback, createContext, useState } from 'react';
|
|
import { LOGIN } from 'store/actions';
|
|
import { useDispatch } from 'react-redux';
|
|
|
|
export const UserContext = createContext();
|
|
|
|
// eslint-disable-next-line
|
|
const UserProvider = ({ children }) => {
|
|
const dispatch = useDispatch();
|
|
const [isUserLoaded, setIsUserLoaded] = useState(false);
|
|
|
|
const loadUser = useCallback(() => {
|
|
let user = localStorage.getItem('user');
|
|
if (user) {
|
|
let data = JSON.parse(user);
|
|
dispatch({ type: LOGIN, payload: data });
|
|
}
|
|
setIsUserLoaded(true);
|
|
}, [dispatch]);
|
|
|
|
useEffect(() => {
|
|
loadUser();
|
|
}, [loadUser]);
|
|
|
|
return <UserContext.Provider value={{ loadUser, isUserLoaded }}> {children} </UserContext.Provider>;
|
|
};
|
|
|
|
export default UserProvider;
|