|
@@ -1,21 +1,20 @@
|
1
|
1
|
const React = require('react');
|
2
|
|
-let ctx = {
|
|
2
|
+const _context = {
|
3
|
3
|
title: 'Title from global context',
|
4
|
4
|
count: 0
|
5
|
5
|
};
|
6
|
|
-
|
7
|
|
-const stateAwareContext = (component) =>
|
8
|
|
- new Proxy(ctx, {
|
9
|
|
- set: function (obj, prop, value) {
|
10
|
|
- obj[prop] = value;
|
11
|
|
- component.setState({ context: stateAwareContext(component) });
|
12
|
|
- return true;
|
13
|
|
- }
|
14
|
|
- });
|
|
6
|
+const contextWrapper = (component) => ({
|
|
7
|
+ ..._context,
|
|
8
|
+ incrementCount: () => {
|
|
9
|
+ _context.count++;
|
|
10
|
+ component.setState({ context: contextWrapper(component) })
|
|
11
|
+ }
|
|
12
|
+});
|
15
|
13
|
|
16
|
14
|
const GlobalContext = React.createContext({});
|
17
|
15
|
class ContextProvider extends React.Component {
|
18
|
|
- state = {context :stateAwareContext(this)};
|
|
16
|
+ state = { context: contextWrapper(this) }
|
|
17
|
+
|
19
|
18
|
render() {
|
20
|
19
|
return (
|
21
|
20
|
<GlobalContext.Provider value={this.state.context}>
|