forked from module-federation/module-federation-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathService.js
More file actions
29 lines (22 loc) · 655 Bytes
/
Service.js
File metadata and controls
29 lines (22 loc) · 655 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import * as React from "react";
export const Context = React.createContext(undefined);
function useService() {
const [service, setService] = React.useState({ title: "" });
return {
...service,
setService,
};
}
export function useServiceContext() {
const context = React.useContext(Context);
if (context === undefined) {
throw new Error(
"ServiceContext value is undefined. Make sure you use the ServiceProvider before using the context."
);
}
return context;
}
export function ServiceProvider(props) {
const value = useService();
return <Context.Provider value={value}>{props.children}</Context.Provider>;
}