Skip to content

Instantly share code, notes, and snippets.

@mhaagens
Last active September 12, 2018 23:56
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mhaagens/61f88e3fbfddbe2c00708f3ebd099be4 to your computer and use it in GitHub Desktop.
Save mhaagens/61f88e3fbfddbe2c00708f3ebd099be4 to your computer and use it in GitHub Desktop.
Transition Motion React Router 4 Route Transition
const FadeRoute = ({ component: Component, ...rest }) => {
return (
<Route {...rest} children={({ location, match }) => (
<TransitionMotion
willEnter={() => {return {opacity: 0, translateX: 24}}}
willLeave={() => {return {opacity: spring(0, animationPresets.out), translateX: spring(24)}}}
defaultStyles={[ {
key: location.pathname,
style: { opacity: 0, translateX: 24},
data: match
} ]}
styles={match ? [ {
key: location.pathname,
style: { opacity: spring(1, animationPresets.in), translateX: spring(0) },
data: match
} ] : []}
>
{interpolatedStyles => (
<div className="route">
{interpolatedStyles.map(config => (
<div
className="page"
key={config.key}
style={{opacity: `${config.style.opacity}`, transform: `translateX(-${config.style.translateX}px)`}}
>
<Component />
</div>
))}
</div>
)}
</TransitionMotion>
)}/>
)
}
export default FadeRoute
@okstaticzero
Copy link

Great! I've been looking all over for a transition example using React Router 4. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment