Skip to content

Commit 6b68721

Browse files
committed
breaking: Use stable context API
1 parent 3a4cf9c commit 6b68721

File tree

5 files changed

+68
-50
lines changed

5 files changed

+68
-50
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
]
5151
},
5252
"peerDependencies": {
53-
"react": ">=15.0.0",
54-
"react-dom": ">=15.0.0"
53+
"react": ">=16.6.0",
54+
"react-dom": ">=16.6.0"
5555
},
5656
"dependencies": {
5757
"dom-helpers": "^3.3.1",
@@ -87,8 +87,8 @@
8787
"husky": "^1.0.0-rc.15",
8888
"jest": "^23.6.0",
8989
"prettier": "^1.14.3",
90-
"react": "^16.5.2",
91-
"react-dom": "^16.5.2",
90+
"react": "~16.6.3",
91+
"react-dom": "~16.6.3",
9292
"react-test-renderer": "^16.5.2",
9393
"release-script": "^1.0.2",
9494
"rimraf": "^2.6.1",

src/Transition.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ReactDOM from 'react-dom'
44
import { polyfill } from 'react-lifecycles-compat'
55

66
import { timeoutsShape } from './utils/PropTypes'
7+
import TransitionGroupContext from './TransitionGroupContext'
78

89
export const UNMOUNTED = 'unmounted'
910
export const EXITED = 'exited'
@@ -107,17 +108,12 @@ export const EXITING = 'exiting'
107108
* > `CSSTransition`.
108109
*/
109110
class Transition extends React.Component {
110-
static contextTypes = {
111-
transitionGroup: PropTypes.object,
112-
}
113-
static childContextTypes = {
114-
transitionGroup: () => {},
115-
}
111+
static contextType = TransitionGroupContext
116112

117113
constructor(props, context) {
118114
super(props, context)
119115

120-
let parentGroup = context.transitionGroup
116+
let parentGroup = context
121117
// In the context of a TransitionGroup all enters are really appears
122118
let appear =
123119
parentGroup && !parentGroup.isMounting ? props.enter : props.appear
@@ -146,10 +142,6 @@ class Transition extends React.Component {
146142
this.nextCallback = null
147143
}
148144

149-
getChildContext() {
150-
return { transitionGroup: null } // allows for nested Transitions
151-
}
152-
153145
static getDerivedStateFromProps({ in: nextIn }, prevState) {
154146
if (nextIn && prevState.status === UNMOUNTED) {
155147
return { status: EXITED }
@@ -236,8 +228,8 @@ class Transition extends React.Component {
236228

237229
performEnter(node, mounting) {
238230
const { enter } = this.props
239-
const appearing = this.context.transitionGroup
240-
? this.context.transitionGroup.isMounting
231+
const appearing = this.context
232+
? this.context.isMounting
241233
: mounting
242234

243235
const timeouts = this.getTimeouts()
@@ -364,11 +356,21 @@ class Transition extends React.Component {
364356
delete childProps.onExited
365357

366358
if (typeof children === 'function') {
367-
return children(status, childProps)
359+
// allows for nested Transitions
360+
return (
361+
<TransitionGroupContext.Provider value={null}>
362+
{children(status, childProps)}
363+
</TransitionGroupContext.Provider>
364+
)
368365
}
369366

370367
const child = React.Children.only(children)
371-
return React.cloneElement(child, childProps)
368+
return (
369+
// allows for nested Transitions
370+
<TransitionGroupContext.Provider value={null}>
371+
{React.cloneElement(child, childProps)}
372+
</TransitionGroupContext.Provider>
373+
)
372374
}
373375
}
374376

src/TransitionGroup.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import PropTypes from 'prop-types'
22
import React from 'react'
33
import { polyfill } from 'react-lifecycles-compat'
4+
import TransitionGroupContext from './TransitionGroupContext'
45

56

67
import {
@@ -31,31 +32,24 @@ const defaultProps = {
3132
* items.
3233
*/
3334
class TransitionGroup extends React.Component {
34-
static childContextTypes = {
35-
transitionGroup: PropTypes.object.isRequired,
36-
}
37-
3835
constructor(props, context) {
3936
super(props, context)
4037

4138
const handleExited = this.handleExited.bind(this)
4239

4340
// Initial children should all be entering, dependent on appear
4441
this.state = {
42+
contextValue: { isMounting: true },
4543
handleExited,
4644
firstRender: true,
4745
}
4846
}
4947

50-
getChildContext() {
51-
return {
52-
transitionGroup: { isMounting: !this.appeared },
53-
}
54-
}
55-
5648
componentDidMount() {
57-
this.appeared = true
5849
this.mounted = true
50+
this.setState({
51+
contextValue: { isMounting: false },
52+
})
5953
}
6054

6155
componentWillUnmount() {
@@ -95,16 +89,25 @@ class TransitionGroup extends React.Component {
9589

9690
render() {
9791
const { component: Component, childFactory, ...props } = this.props
92+
const { contextValue } = this.state
9893
const children = values(this.state.children).map(childFactory)
9994

10095
delete props.appear
10196
delete props.enter
10297
delete props.exit
10398

10499
if (Component === null) {
105-
return children
100+
return (
101+
<TransitionGroupContext.Provider value={contextValue}>
102+
{children}
103+
</TransitionGroupContext.Provider>
104+
)
106105
}
107-
return <Component {...props}>{children}</Component>
106+
return (
107+
<TransitionGroupContext.Provider value={contextValue}>
108+
<Component {...props}>{children}</Component>
109+
</TransitionGroupContext.Provider>
110+
)
108111
}
109112
}
110113

src/TransitionGroupContext.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React from 'react';
2+
3+
export default React.createContext(null);

yarn.lock

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9413,15 +9413,6 @@ react-docgen@^3.0.0-rc.1:
94139413
node-dir "^0.1.10"
94149414
recast "^0.16.0"
94159415

9416-
react-dom@^16.5.2:
9417-
version "16.5.2"
9418-
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.5.2.tgz#b69ee47aa20bab5327b2b9d7c1fe2a30f2cfa9d7"
9419-
dependencies:
9420-
loose-envify "^1.1.0"
9421-
object-assign "^4.1.1"
9422-
prop-types "^15.6.2"
9423-
schedule "^0.5.0"
9424-
94259416
react-dom@^16.6.3:
94269417
version "16.7.0"
94279418
resolved "http://storage.mds.yandex.net/get-npm/38095/react-dom-16.7.0.tgz#a17b2a7ca89ee7390bc1ed5eb81783c7461748b8"
@@ -9432,6 +9423,16 @@ react-dom@^16.6.3:
94329423
prop-types "^15.6.2"
94339424
scheduler "^0.12.0"
94349425

9426+
react-dom@~16.6.0:
9427+
version "16.6.3"
9428+
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.6.3.tgz#8fa7ba6883c85211b8da2d0efeffc9d3825cccc0"
9429+
integrity sha512-8ugJWRCWLGXy+7PmNh8WJz3g1TaTUt1XyoIcFN+x0Zbkoz+KKdUyx1AQLYJdbFXjuF41Nmjn5+j//rxvhFjgSQ==
9430+
dependencies:
9431+
loose-envify "^1.1.0"
9432+
object-assign "^4.1.1"
9433+
prop-types "^15.6.2"
9434+
scheduler "^0.11.2"
9435+
94359436
react-error-overlay@^5.1.0:
94369437
version "5.1.1"
94379438
resolved "http://storage.mds.yandex.net/get-npm/69187/react-error-overlay-5.1.1.tgz#56f0439f001ff3588da0f479a86482ccb1e708cb"
@@ -9541,15 +9542,6 @@ react-treebeard@^3.1.0:
95419542
shallowequal "^1.1.0"
95429543
velocity-react "^1.4.1"
95439544

9544-
react@^16.5.2:
9545-
version "16.5.2"
9546-
resolved "https://registry.yarnpkg.com/react/-/react-16.5.2.tgz#19f6b444ed139baa45609eee6dc3d318b3895d42"
9547-
dependencies:
9548-
loose-envify "^1.1.0"
9549-
object-assign "^4.1.1"
9550-
prop-types "^15.6.2"
9551-
schedule "^0.5.0"
9552-
95539545
react@^16.6.3:
95549546
version "16.7.0"
95559547
resolved "http://storage.mds.yandex.net/get-npm/45674/react-16.7.0.tgz#b674ec396b0a5715873b350446f7ea0802ab6381"
@@ -9560,6 +9552,16 @@ react@^16.6.3:
95609552
prop-types "^15.6.2"
95619553
scheduler "^0.12.0"
95629554

9555+
react@~16.6.0:
9556+
version "16.6.3"
9557+
resolved "https://registry.yarnpkg.com/react/-/react-16.6.3.tgz#25d77c91911d6bbdd23db41e70fb094cc1e0871c"
9558+
integrity sha512-zCvmH2vbEolgKxtqXL2wmGCUxUyNheYn/C+PD1YAjfxHC54+MhdruyhO7QieQrYsYeTxrn93PM2y0jRH1zEExw==
9559+
dependencies:
9560+
loose-envify "^1.1.0"
9561+
object-assign "^4.1.1"
9562+
prop-types "^15.6.2"
9563+
scheduler "^0.11.2"
9564+
95639565
read-cmd-shim@^1.0.1, read-cmd-shim@~1.0.1:
95649566
version "1.0.1"
95659567
resolved "https://registry.yarnpkg.com/read-cmd-shim/-/read-cmd-shim-1.0.1.tgz#2d5d157786a37c055d22077c32c53f8329e91c7b"
@@ -10251,6 +10253,14 @@ schedule@^0.5.0:
1025110253
dependencies:
1025210254
object-assign "^4.1.1"
1025310255

10256+
scheduler@^0.11.2:
10257+
version "0.11.3"
10258+
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.11.3.tgz#b5769b90cf8b1464f3f3cfcafe8e3cd7555a2d6b"
10259+
integrity sha512-i9X9VRRVZDd3xZw10NY5Z2cVMbdYg6gqFecfj79USv1CFN+YrJ3gIPRKf1qlY+Sxly4djoKdfx1T+m9dnRB8kQ==
10260+
dependencies:
10261+
loose-envify "^1.1.0"
10262+
object-assign "^4.1.1"
10263+
1025410264
scheduler@^0.12.0:
1025510265
version "0.12.0"
1025610266
resolved "http://storage.mds.yandex.net/get-npm/45674/scheduler-0.12.0.tgz#8ab17699939c0aedc5a196a657743c496538647b"

0 commit comments

Comments
 (0)