Skip to content

Commit 1666ef4

Browse files
committed
Fix the animations example
1 parent 1bfce34 commit 1666ef4

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

examples/animations/app.js

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react'
22
import { render } from 'react-dom'
33
import ReactCSSTransitionGroup from 'react-addons-css-transition-group'
4+
import StaticContainer from 'react-static-container'
45
import { createHistory, useBasename } from 'history'
56
import { Router, Route, Link } from 'react-router'
67

@@ -10,23 +11,62 @@ const history = useBasename(createHistory)({
1011
basename: '/animations'
1112
})
1213

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+
1433
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+
}
1648

49+
componentDidUpdate() {
50+
if (this.state.previousPathname) {
51+
this.setState({ previousPathname: null })
52+
}
53+
}
54+
}
55+
56+
class App extends React.Component {
57+
render() {
1758
return (
1859
<div>
1960
<ul>
2061
<li><Link to="/page1">Page 1</Link></li>
2162
<li><Link to="/page2">Page 2</Link></li>
2263
</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}
2767
>
28-
{React.cloneElement(this.props.children || <div />, { key: pathname })}
29-
</ReactCSSTransitionGroup>
68+
{this.props.children}
69+
</RouteCSSTransitionGroup>
3070
</div>
3171
)
3272
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"react-addons-css-transition-group": "^0.14.0",
6565
"react-addons-test-utils": "0.14.0",
6666
"react-dom": "^0.14.0",
67+
"react-static-container": "^1.0.0",
6768
"rf-changelog": "^0.4.0",
6869
"style-loader": "^0.12.4",
6970
"webpack": "^1.4.13",

0 commit comments

Comments
 (0)