Skip to content

Instantly share code, notes, and snippets.

@zilahir
Created April 23, 2020 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zilahir/fdffafbfec5d9597b448f43af0343f22 to your computer and use it in GitHub Desktop.
Save zilahir/fdffafbfec5d9597b448f43af0343f22 to your computer and use it in GitHub Desktop.
routeTransition
import React from 'react'
import { AnimatePresence } from 'framer-motion'
import { useSelector } from 'react-redux'
import { Route, Switch, useLocation, Redirect } from 'react-router-dom'
import { MountTransition } from '../MountTransition'
export const RouteTransition = ({
children,
exact = false,
path,
slide = 0,
slideUp = 0,
...rest
}) => {
const authUser = useSelector(store => store.authUser.user.authSuccess)
if (authUser) {
return (
<Route exact={exact} path={path} {...rest}>
<MountTransition slide={slide} slideUp={slideUp}>
{children}
</MountTransition>
</Route>
)
} else {
return (
<Redirect
to={{
pathname: '/login',
state: {
demo: true,
}
}}
/>
)
}
}
export const AnimatedRoutes = ({
children,
exitBeforeEnter = true,
initial = false,
}) => {
const location = useLocation()
return (
<AnimatePresence exitBeforeEnter={exitBeforeEnter} initial={initial}>
<Switch location={location} key={location.pathname}>
{children}
</Switch>
</AnimatePresence>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment