Skip to content

Commit f175957

Browse files
committed
breaking: Use stable context API
1 parent 2eb45b2 commit f175957

File tree

5 files changed

+65
-53
lines changed

5 files changed

+65
-53
lines changed

package.json

+4-4
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.4.0",
@@ -86,8 +86,8 @@
8686
"husky": "^1.3.1",
8787
"jest": "^24.7.1",
8888
"prettier": "^1.16.4",
89-
"react": "^16.5.2",
90-
"react-dom": "^16.5.2",
89+
"react": "~16.6.3",
90+
"react-dom": "~16.6.3",
9191
"release-script": "^1.0.2",
9292
"rimraf": "^2.6.3",
9393
"rollup": "^1.9.0",

src/Transition.js

+17-15
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'
@@ -101,17 +102,12 @@ export const EXITING = 'exiting'
101102
* `'exiting'` to `'exited'`.
102103
*/
103104
class Transition extends React.Component {
104-
static contextTypes = {
105-
transitionGroup: PropTypes.object,
106-
}
107-
static childContextTypes = {
108-
transitionGroup: () => {},
109-
}
105+
static contextType = TransitionGroupContext
110106

111107
constructor(props, context) {
112108
super(props, context)
113109

114-
let parentGroup = context.transitionGroup
110+
let parentGroup = context
115111
// In the context of a TransitionGroup all enters are really appears
116112
let appear =
117113
parentGroup && !parentGroup.isMounting ? props.enter : props.appear
@@ -140,10 +136,6 @@ class Transition extends React.Component {
140136
this.nextCallback = null
141137
}
142138

143-
getChildContext() {
144-
return { transitionGroup: null } // allows for nested Transitions
145-
}
146-
147139
static getDerivedStateFromProps({ in: nextIn }, prevState) {
148140
if (nextIn && prevState.status === UNMOUNTED) {
149141
return { status: EXITED }
@@ -230,8 +222,8 @@ class Transition extends React.Component {
230222

231223
performEnter(node, mounting) {
232224
const { enter } = this.props
233-
const appearing = this.context.transitionGroup
234-
? this.context.transitionGroup.isMounting
225+
const appearing = this.context
226+
? this.context.isMounting
235227
: mounting
236228

237229
const timeouts = this.getTimeouts()
@@ -358,11 +350,21 @@ class Transition extends React.Component {
358350
delete childProps.onExited
359351

360352
if (typeof children === 'function') {
361-
return children(status, childProps)
353+
// allows for nested Transitions
354+
return (
355+
<TransitionGroupContext.Provider value={null}>
356+
{children(status, childProps)}
357+
</TransitionGroupContext.Provider>
358+
)
362359
}
363360

364361
const child = React.Children.only(children)
365-
return React.cloneElement(child, childProps)
362+
return (
363+
// allows for nested Transitions
364+
<TransitionGroupContext.Provider value={null}>
365+
{React.cloneElement(child, childProps)}
366+
</TransitionGroupContext.Provider>
367+
)
366368
}
367369
}
368370

src/TransitionGroup.js

+16-13
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

+3
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

+25-21
Original file line numberDiff line numberDiff line change
@@ -9926,15 +9926,6 @@ react-docgen@^3.0.0-rc.1:
99269926
node-dir "^0.1.10"
99279927
recast "^0.16.0"
99289928

9929-
react-dom@^16.5.2:
9930-
version "16.5.2"
9931-
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.5.2.tgz#b69ee47aa20bab5327b2b9d7c1fe2a30f2cfa9d7"
9932-
dependencies:
9933-
loose-envify "^1.1.0"
9934-
object-assign "^4.1.1"
9935-
prop-types "^15.6.2"
9936-
schedule "^0.5.0"
9937-
99389929
react-dom@^16.6.3:
99399930
version "16.7.0"
99409931
resolved "http://storage.mds.yandex.net/get-npm/38095/react-dom-16.7.0.tgz#a17b2a7ca89ee7390bc1ed5eb81783c7461748b8"
@@ -9945,6 +9936,16 @@ react-dom@^16.6.3:
99459936
prop-types "^15.6.2"
99469937
scheduler "^0.12.0"
99479938

9939+
react-dom@~16.6.3:
9940+
version "16.6.3"
9941+
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.6.3.tgz#8fa7ba6883c85211b8da2d0efeffc9d3825cccc0"
9942+
integrity sha512-8ugJWRCWLGXy+7PmNh8WJz3g1TaTUt1XyoIcFN+x0Zbkoz+KKdUyx1AQLYJdbFXjuF41Nmjn5+j//rxvhFjgSQ==
9943+
dependencies:
9944+
loose-envify "^1.1.0"
9945+
object-assign "^4.1.1"
9946+
prop-types "^15.6.2"
9947+
scheduler "^0.11.2"
9948+
99489949
react-error-overlay@^5.1.0:
99499950
version "5.1.1"
99509951
resolved "http://storage.mds.yandex.net/get-npm/69187/react-error-overlay-5.1.1.tgz#56f0439f001ff3588da0f479a86482ccb1e708cb"
@@ -10046,15 +10047,6 @@ react-treebeard@^3.1.0:
1004610047
shallowequal "^1.1.0"
1004710048
velocity-react "^1.4.1"
1004810049

10049-
react@^16.5.2:
10050-
version "16.5.2"
10051-
resolved "https://registry.yarnpkg.com/react/-/react-16.5.2.tgz#19f6b444ed139baa45609eee6dc3d318b3895d42"
10052-
dependencies:
10053-
loose-envify "^1.1.0"
10054-
object-assign "^4.1.1"
10055-
prop-types "^15.6.2"
10056-
schedule "^0.5.0"
10057-
1005810050
react@^16.6.3:
1005910051
version "16.7.0"
1006010052
resolved "http://storage.mds.yandex.net/get-npm/45674/react-16.7.0.tgz#b674ec396b0a5715873b350446f7ea0802ab6381"
@@ -10065,6 +10057,16 @@ react@^16.6.3:
1006510057
prop-types "^15.6.2"
1006610058
scheduler "^0.12.0"
1006710059

10060+
react@~16.6.3:
10061+
version "16.6.3"
10062+
resolved "https://registry.yarnpkg.com/react/-/react-16.6.3.tgz#25d77c91911d6bbdd23db41e70fb094cc1e0871c"
10063+
integrity sha512-zCvmH2vbEolgKxtqXL2wmGCUxUyNheYn/C+PD1YAjfxHC54+MhdruyhO7QieQrYsYeTxrn93PM2y0jRH1zEExw==
10064+
dependencies:
10065+
loose-envify "^1.1.0"
10066+
object-assign "^4.1.1"
10067+
prop-types "^15.6.2"
10068+
scheduler "^0.11.2"
10069+
1006810070
read-cmd-shim@^1.0.1, read-cmd-shim@~1.0.1:
1006910071
version "1.0.1"
1007010072
resolved "https://registry.yarnpkg.com/read-cmd-shim/-/read-cmd-shim-1.0.1.tgz#2d5d157786a37c055d22077c32c53f8329e91c7b"
@@ -10772,10 +10774,12 @@ sax@^1.2.4, sax@~1.2.4:
1077210774
version "1.2.4"
1077310775
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
1077410776

10775-
schedule@^0.5.0:
10776-
version "0.5.0"
10777-
resolved "https://registry.yarnpkg.com/schedule/-/schedule-0.5.0.tgz#c128fffa0b402488b08b55ae74bb9df55cc29cc8"
10777+
scheduler@^0.11.2:
10778+
version "0.11.3"
10779+
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.11.3.tgz#b5769b90cf8b1464f3f3cfcafe8e3cd7555a2d6b"
10780+
integrity sha512-i9X9VRRVZDd3xZw10NY5Z2cVMbdYg6gqFecfj79USv1CFN+YrJ3gIPRKf1qlY+Sxly4djoKdfx1T+m9dnRB8kQ==
1077810781
dependencies:
10782+
loose-envify "^1.1.0"
1077910783
object-assign "^4.1.1"
1078010784

1078110785
scheduler@^0.12.0:

0 commit comments

Comments
 (0)