-
Notifications
You must be signed in to change notification settings - Fork 1
/
GlobalContext.js
44 lines (37 loc) · 946 Bytes
/
GlobalContext.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import React from "react";
const GlobalContext = React.createContext({ images: [], detailImage: null });
class GlobalContextProvider extends React.Component {
state = {
images: [],
detailImage: null
};
updateImages = images => {
this.setState({ images });
};
updateDetailImage = detailImage => {
this.setState({ detailImage });
};
render() {
return (
<GlobalContext.Provider
value={{
...this.state,
updateImages: this.updateImages,
updateDetailImage: this.updateDetailImage
}}
>
{this.props.children}
</GlobalContext.Provider>
);
}
}
// create the consumer as higher order component
const withGlobalContext = ChildComponent => props => (
<GlobalContext.Consumer>
{context => <ChildComponent {...props} global={context} />}
</GlobalContext.Consumer>
);
module.exports = {
GlobalContextProvider,
withGlobalContext
};