@@ -30,6 +30,9 @@ function areStatePropsEqual(stateProps, nextStateProps) {
3030 return shallowEqual ( stateProps , nextStateProps ) ;
3131}
3232
33+ // Helps track hot reloading.
34+ let nextVersion = 0 ;
35+
3336export default function createConnect ( React ) {
3437 const { Component, PropTypes } = React ;
3538 const storeShape = createStoreShape ( PropTypes ) ;
@@ -44,6 +47,9 @@ export default function createConnect(React) {
4447 wrapActionCreators ( actionCreatorsOrMapDispatchToProps ) :
4548 actionCreatorsOrMapDispatchToProps ;
4649
50+ // Helps track hot reloading.
51+ const version = nextVersion ++ ;
52+
4753 return DecoratedComponent => class Connect extends Component {
4854 static displayName = `Connect(${ getDisplayName ( DecoratedComponent ) } )` ;
4955 static DecoratedComponent = DecoratedComponent ;
@@ -61,6 +67,7 @@ export default function createConnect(React) {
6167
6268 constructor ( props , context ) {
6369 super ( props , context ) ;
70+ this . version = version ;
6471 this . setUnderlyingRef = ::this . setUnderlyingRef ;
6572 this . state = {
6673 ...this . mapState ( props , context ) ,
@@ -72,18 +79,45 @@ export default function createConnect(React) {
7279 return typeof this . unsubscribe === 'function' ;
7380 }
7481
75- componentDidMount ( ) {
76- if ( shouldSubscribe ) {
82+ trySubscribe ( ) {
83+ if ( shouldSubscribe && ! this . unsubscribe ) {
7784 this . unsubscribe = this . context . store . subscribe ( ::this . handleChange ) ;
7885 }
7986 }
8087
81- componentWillUnmount ( ) {
88+ tryUnsubscribe ( ) {
8289 if ( this . isSubscribed ( ) ) {
8390 this . unsubscribe ( ) ;
91+ this . unsubscribe = null ;
8492 }
8593 }
8694
95+ componentDidMount ( ) {
96+ this . trySubscribe ( ) ;
97+ }
98+
99+ componentWillUpdate ( ) {
100+ if ( process . env . NODE_ENV !== 'production' ) {
101+ if ( this . version === version ) {
102+ return ;
103+ }
104+
105+ // We are hot reloading!
106+ this . version = version ;
107+
108+ // Update the state and bindings.
109+ this . trySubscribe ( ) ;
110+ this . setState ( {
111+ ...this . mapState ( ) ,
112+ ...this . mapDispatch ( )
113+ } ) ;
114+ }
115+ }
116+
117+ componentWillUnmount ( ) {
118+ this . tryUnsubscribe ( ) ;
119+ }
120+
87121 handleChange ( props = this . props ) {
88122 const nextState = this . mapState ( props , this . context ) ;
89123 if ( ! areStatePropsEqual ( this . state . stateProps , nextState . stateProps ) ) {
0 commit comments