lint
This commit is contained in:
@@ -3,3 +3,5 @@ description: Provider 模版
|
||||
globs: ["**/*.ts", "**/*.tsx"]
|
||||
alwaysApply: Auto Attached
|
||||
---
|
||||
|
||||
- RouterProvider 为 react-router 路由配置文件
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
// @rule: do not modify this file
|
||||
|
||||
import { MujianSdk } from '@mujian/js-sdk';
|
||||
import {
|
||||
createContext,
|
||||
type ReactNode,
|
||||
useContext,
|
||||
useEffect,
|
||||
useState,
|
||||
} from 'react';
|
||||
|
||||
export { EVENT } from '@mujian/js-sdk';
|
||||
|
||||
const MujianContext = createContext<MujianSdk | null>(null);
|
||||
|
||||
export interface MujianProviderProps {
|
||||
children: ReactNode;
|
||||
loadingPage?: ReactNode;
|
||||
}
|
||||
|
||||
export const MujianProvider = ({
|
||||
children,
|
||||
loadingPage,
|
||||
}: MujianProviderProps) => {
|
||||
const [mujian, setMujian] = useState<MujianSdk | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
const mujian = new MujianSdk();
|
||||
await mujian.init();
|
||||
setMujian(mujian);
|
||||
})();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<MujianContext.Provider value={mujian}>
|
||||
{mujian ? children : (loadingPage ?? 'Mujian is loading')}
|
||||
</MujianContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useMujian = () => {
|
||||
const mujian = useContext(MujianContext);
|
||||
if (!mujian) {
|
||||
const message =
|
||||
'Mujian is not initialized. Please check that useMujian is called inside MujianProvider';
|
||||
console.error(message);
|
||||
throw new Error(message);
|
||||
}
|
||||
return mujian;
|
||||
};
|
||||
Reference in New Issue
Block a user