Last active
January 31, 2016 23:46
-
-
Save zackify/e57419e73d7c7ca79815 to your computer and use it in GitHub Desktop.
transition in react router demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
export default class Transition extends React.Component{ | |
static contextTypes = { | |
router: React.PropTypes.object | |
}; | |
constructor(props){ | |
super() | |
let opacity = 1 | |
if(props.in !== false){ | |
if(props.location.action == 'PUSH') opacity = 0.2 | |
else if(process.CLIENT && props.location.action == 'POP') opacity = 0.2 | |
} | |
this.state = { | |
opacity | |
} | |
} | |
componentDidMount() { | |
if(this.state.opacity == 0.2) this.fadeIn() | |
if(this.props.out !== false) this.context.router.setRouteLeaveHook(this.props.route, this.routerWillLeave.bind(this)) | |
} | |
routerWillLeave() { | |
this.setState({opacity: 0.2}) | |
} | |
fadeIn() { | |
setTimeout(() => this.setState({opacity: 1}),50) | |
} | |
render(){ | |
let style = { | |
transition: 'all 500ms ease-in-out', | |
...this.state | |
} | |
return ( | |
<div style={style}> | |
{this.props.children} | |
</div> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't need those hooks: