/
Provider.js
93 lines (75 loc) · 2.58 KB
/
Provider.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
import React, { Component, Children } from 'react'
import PropTypes from 'prop-types'
import { storeShape, subscriptionShape } from '../utils/PropTypes'
import warning from '../utils/warning'
import {ReactReduxContext} from "./context";
let didWarnAboutReceivingStore = false
function warnAboutReceivingStore() {
if (didWarnAboutReceivingStore) {
return
}
didWarnAboutReceivingStore = true
warning(
'<Provider> does not support changing `store` on the fly. ' +
'It is most likely that you see this error because you updated to ' +
'Redux 2.x and React Redux 2.x which no longer hot reload reducers ' +
'automatically. See https://github.com/reactjs/react-redux/releases/' +
'tag/v2.0.0 for the migration instructions.'
)
}
export function createProvider(storeKey = 'store', subKey) {
//const subscriptionKey = subKey || `${storeKey}Subscription`
class Provider extends Component {
/*
getChildContext() {
return { [storeKey]: this[storeKey], [subscriptionKey]: null }
}
*/
constructor(props, context) {
super(props, context)
//this[storeKey] = props.store;
const {store} = props;
if(!store || !store.getState || !store.dispatch) {
throw new Error("Must pass a valid Redux store as a prop to Provider");
}
this.state = {
storeState : store.getState(),
dispatch : store.dispatch,
};
}
componentDidMount() {
const {store} = this.props;
this.unsubscribe = store.subscribe( () => {
console.log("Provider subscription running");
this.setState({storeState : store.getState()});
});
}
render() {
console.log("Provider re-rendering");
return (
<ReactReduxContext.Provider value={this.state}>
{Children.only(this.props.children)}
</ReactReduxContext.Provider>
);
}
}
if (process.env.NODE_ENV !== 'production') {
Provider.prototype.componentWillReceiveProps = function (nextProps) {
if (this[storeKey] !== nextProps.store) {
warnAboutReceivingStore()
}
}
}
Provider.propTypes = {
store: storeShape.isRequired,
children: PropTypes.element.isRequired,
}
/*
Provider.childContextTypes = {
[storeKey]: storeShape.isRequired,
[subscriptionKey]: subscriptionShape,
}
*/
return Provider
}
export default createProvider()