1
1
import React from 'react'
2
2
import { render } from 'react-dom'
3
3
import ReactCSSTransitionGroup from 'react-addons-css-transition-group'
4
+ import StaticContainer from 'react-static-container'
4
5
import { createHistory , useBasename } from 'history'
5
6
import { Router , Route , Link } from 'react-router'
6
7
@@ -10,23 +11,62 @@ const history = useBasename(createHistory)({
10
11
basename : '/animations'
11
12
} )
12
13
13
- class App extends React . Component {
14
+ class RouteCSSTransitionGroup extends React . Component {
15
+ static contextTypes = {
16
+ location : React . PropTypes . object
17
+ }
18
+
19
+ constructor ( props , context ) {
20
+ super ( props , context )
21
+
22
+ this . state = {
23
+ previousPathname : null
24
+ }
25
+ }
26
+
27
+ componentWillReceiveProps ( nextProps , nextContext ) {
28
+ if ( nextContext . location . pathname !== this . context . location . pathname ) {
29
+ this . setState ( { previousPathname : this . context . location . pathname } )
30
+ }
31
+ }
32
+
14
33
render ( ) {
15
- const { pathname } = this . props . location
34
+ const { children, ...props } = this . props
35
+ const { previousPathname } = this . state
36
+
37
+ return (
38
+ < ReactCSSTransitionGroup { ...props } >
39
+ < StaticContainer
40
+ key = { previousPathname || this . context . location . pathname }
41
+ shouldUpdate = { ! previousPathname }
42
+ >
43
+ { children }
44
+ </ StaticContainer >
45
+ </ ReactCSSTransitionGroup >
46
+ )
47
+ }
16
48
49
+ componentDidUpdate ( ) {
50
+ if ( this . state . previousPathname ) {
51
+ this . setState ( { previousPathname : null } )
52
+ }
53
+ }
54
+ }
55
+
56
+ class App extends React . Component {
57
+ render ( ) {
17
58
return (
18
59
< div >
19
60
< ul >
20
61
< li > < Link to = "/page1" > Page 1</ Link > </ li >
21
62
< li > < Link to = "/page2" > Page 2</ Link > </ li >
22
63
</ ul >
23
- < ReactCSSTransitionGroup component = "div"
24
- transitionName = "example"
25
- transitionEnterTimeout = { 500 }
26
- transitionLeaveTimeout = { 500 }
64
+ < RouteCSSTransitionGroup
65
+ component = "div" transitionName = "example"
66
+ transitionEnterTimeout = { 500 } transitionLeaveTimeout = { 500 }
27
67
>
28
- { React . cloneElement ( this . props . children || < div /> , { key : pathname } ) }
29
- </ ReactCSSTransitionGroup >
68
+ { this . props . children }
69
+ </ RouteCSSTransitionGroup >
30
70
</ div >
31
71
)
32
72
}
0 commit comments