-
Notifications
You must be signed in to change notification settings - Fork 1
/
redux-react.js
103 lines (75 loc) · 2.04 KB
/
redux-react.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'use strict';
module.exports = {
createProvider: createProvider,
createConnect: createConnect
};
function createProvider (React) {
const Component = React.Component;
class Provider extends Component {
constructor (props, context) {
super(props, context);
this.store = props.store;
}
getChildContext () {
return { store: this.store };
}
componentWillReceiveProps (nextProps) {
const store = this.store;
const nextStore = nextProps.store;
}
render () {
let children = this.props.children;
if (typeof children === 'function') {
// console.log('warnAboutFunctionChild()');
children = children();
} else {
// console.log('warnAboutElementChild();');
}
return children;
}
}
Provider.childContextTypes = {
store: React.PropTypes.object
};
return Provider;
}
function createConnect (React) {
const Component = React.Component;
return function connect (mapStateToProps) {
return function wrapWithConnect (WrappedComponent) {
class Connect extends Component {
constructor (props, context) {
super(props, context);
this.handleChange = this.handleChange.bind(this);
this.trySubscribe = this.trySubscribe.bind(this);
this.state = { storeState: null };
this.store = context.store;
}
componentDidMount () {
this.trySubscribe();
}
trySubscribe () {
this.store.subscribe(this.handleChange);
this.handleChange();
}
handleChange () {
this.setState({
storeState: this.store.getState()
});
}
render () {
console.log('Render Connect with store:', this.storeState);
return (
React.createElement(WrappedComponent, {
store: this.storeState
})
);
}
}
Connect.contextTypes = {
store: React.PropTypes.object
};
return Connect;
}
}
}