Skip to content

Commit f41df99

Browse files
jimbollatimdorr
authored andcommitted
Adds react15CompatibilityMode setting (#540)
* Adds react15CompatibilityMode setting as temporary fix for #525 * Remove Provider API for react15CompatibilityMode. * Fix tests against react15CompatibilityMode. * adds defaultReact15CompatibilityMode setting and setDefaultReact15CompatibilityMode function.
1 parent 582b17f commit f41df99

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

src/components/connectAdvanced.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { Component, PropTypes, createElement } from 'react'
55
import Subscription from '../utils/Subscription'
66
import storeShape from '../utils/storeShape'
77

8+
9+
let defaultReact15CompatibilityMode = true
810
let hotReloadingVersion = 0
911
export default function connectAdvanced(
1012
/*
@@ -18,7 +20,7 @@ export default function connectAdvanced(
1820
1921
Access to dispatch is provided to the factory so selectorFactories can bind actionCreators
2022
outside of their selector as an optimization. Options passed to connectAdvanced are passed to
21-
the selectorFactory, along with displayName and WrappedComponent, as the second argument.
23+
the selectorFactory, along with displayName and WrappedComponent, as the second argument.
2224
2325
Note that selectorFactory is responsible for all caching/memoization of inbound and outbound
2426
props. Do not use connectAdvanced directly without memoizing results between calls to your
@@ -35,6 +37,9 @@ export default function connectAdvanced(
3537
// probably overridden by wrapper functions such as connect()
3638
methodName = 'connectAdvanced',
3739

40+
// temporary compatibility setting for React 15. See Connect constructor for details
41+
react15CompatibilityMode = undefined,
42+
3843
// if defined, the name of the property passed to the wrapped element indicating the number of
3944
// calls to render. useful for watching in react devtools for unnecessary re-renders.
4045
renderCountProp = undefined,
@@ -57,11 +62,12 @@ export default function connectAdvanced(
5762

5863
const contextTypes = {
5964
[storeKey]: storeShape,
60-
[subscriptionKey]: PropTypes.instanceOf(Subscription)
65+
[subscriptionKey]: PropTypes.instanceOf(Subscription),
66+
react15CompatibilityMode: PropTypes.bool,
6167
}
6268
const childContextTypes = {
6369
[subscriptionKey]: PropTypes.instanceOf(Subscription)
64-
}
70+
}
6571

6672
return function wrapWithConnect(WrappedComponent) {
6773
invariant(
@@ -97,7 +103,19 @@ export default function connectAdvanced(
97103
this.state = {}
98104
this.renderCount = 0
99105
this.store = this.props[storeKey] || this.context[storeKey]
100-
this.parentSub = this.props[subscriptionKey] || this.context[subscriptionKey]
106+
107+
// react15CompatibilityMode controls whether the subscription system is used. This is for
108+
// https://github.com/reactjs/react-redux/issues/525 and should be removed completely when
109+
// react-redux's dependency on react is bumped to mimimum v16, which is expected to include
110+
// PR https://github.com/facebook/react/pull/8204 which fixes the issue.
111+
const compatMode = [
112+
react15CompatibilityMode,
113+
props.react15CompatibilityMode,
114+
context.react15CompatibilityMode,
115+
defaultReact15CompatibilityMode
116+
].find(cm => cm !== undefined && cm !== null)
117+
this.parentSub = compatMode ? null : props[subscriptionKey] || context[subscriptionKey]
118+
101119
this.setWrappedInstance = this.setWrappedInstance.bind(this)
102120

103121
invariant(this.store,
@@ -257,3 +275,9 @@ export default function connectAdvanced(
257275
return hoistStatics(Connect, WrappedComponent)
258276
}
259277
}
278+
279+
connectAdvanced.setDefaultReact15CompatibilityMode =
280+
function setDefaultReact15CompatibilityMode(compat) {
281+
defaultReact15CompatibilityMode = compat
282+
}
283+

src/connect/connect.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,6 @@ export function createConnect({
8787
}
8888
}
8989

90-
export default createConnect()
90+
const connect = createConnect()
91+
connect.setDefaultReact15CompatibilityMode = connectAdvanced.setDefaultReact15CompatibilityMode
92+
export default connect

test/components/Provider.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ describe('React', () => {
144144
// The state from parent props should always be consistent with the current state
145145
expect(state).toEqual(parentProps.parentState)
146146
return {}
147-
})
147+
}, null, null, { react15CompatibilityMode: false })
148148
class ChildContainer extends Component {
149149
render() {
150150
return <div />

test/components/connect.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1697,7 +1697,7 @@ describe('React', () => {
16971697
// The state from parent props should always be consistent with the current state
16981698
expect(state).toEqual(parentProps.parentState)
16991699
return {}
1700-
})
1700+
}, null, null, { react15CompatibilityMode: false })
17011701
class ChildContainer extends Component {
17021702
render() {
17031703
return <Passthrough {...this.props}/>

0 commit comments

Comments
 (0)