React Router complex dynamic nested routes - react-router

I´m trying to create a complex routing that behaves the following:
http://localhost:3000/dashboard/projectxxxxx/schedule
http://localhost:3000/dashboard/projectxxxxx/game-servers
http://localhost:3000/dashboard/projectyyyyy/schedule
http://localhost:3000/dashboard/projectyyyyy/game-servers
So I need to set the dynamic path first and later on, another nested route. So far, what I have is this following code, but I´m stuck when it comes to get first the project. Could someone help me?
I´m using React Router v6
Thanks a lot!
<Routes>
<Route path="/" element={ <Navigate to="/dashboard" /> } />
<Route path="dashboard" element={ <DashboardPage open={ open } /> }>
<Route index element={ <SchedulePage /> } />
<Route path="schedule" element={ <SchedulePage /> } />
<Route path="game-servers" element={ <GameServersPage /> } />
</Route>
<Route path="*" element={ <NoContentPage /> } />
</Routes>

Related

Transition between routes in react-router-dom v6.3

So I´m currently refactoring a website, and so I do with the rrd, which was on v5 in the previous website version.
Now, that the component doesn´t exist anymore we have to work with the new component as you probably know.
I previously used framer-motion to transition in and out between the routes like so:
<Switch location={location} key={location.pathname}>
<motion.div
initial="initial"
animate="in"
exit="out"
variants={pageVariants}
transition={pageTransition}>
<Route path="/audit" component={Audit} />
<Route exact path="/smartx" component={SmartX} />
<Route path="/smartx/erc20" component={TokenGenerator} />
<Route path="/createAudit" component={PaymentStatus} />
<Route path="/faq" component={FAQ} />
<Route path="/support" component={Support} />
<Route path="/terms" component={Terms} />
<Route path="/policy" component={Policy} />
<Route path="/about" component={About} />
<Route exact path="/">
<Redirect to="/audit" />
</Route>
</motion.div>
</Switch>;
Simply replacing the Switch component with the Routes component won´t work, since you can only have Route components as childs from .
Moving the <motion.div> one layer up over the Routes component leads to only one initial fade in transition on page load.
new (not quiet working version):
<AnimatePresence>
<PageLayout>
<motion.div
initial="initial"
animate="in"
variants={pageVariants}
transition={pageTransition}>
<Routes>
<Route path="/audit" element={<Audit />} />
<Route path="/smartx" element={<SmartX />} />
<Route path="/faq" element={<FAQ />} />
<Route path="/support" element={<Support />} />
<Route path="/terms" element={<Terms />} />
<Route path="/policy" element={<Policy />} />
<Route path="/about" element={<About />} />
</Routes>
</motion.div>
</PageLayout>
</AnimatePresence>;
Framer motion animations (equalin both old and new version):
const pageVariants = {
initial: {
opacity: 0
},
in: {
opacity: 1
},
out: {
opacity: 0
}
};
const pageTransition = {
type: 'tween',
ease: 'linear',
duration: 0.5
};
Any ideas on how to achieve a transition on each route switch?
Thanks in advance and cheers!
I think your solution is close to working. Move the PageLayout component and motion.div into a layout route that uses the current path as a React key.
Example:
import { Outlet } from 'react-router-dom';
const AnimationLayout = () => {
const { pathname } = useLocation();
return (
<PageLayout>
<motion.div
key={pathname}
initial="initial"
animate="in"
variants={pageVariants}
transition={pageTransition}
>
<Outlet />
</motion.div>
</PageLayout>
);
};
...
<Routes>
<Route element={<AnimationLayout />}>
<Route path="/audit" element={<Audit />} />
<Route path="/smartx" element={<SmartX />} />
<Route path="/faq" element={<FAQ />} />
<Route path="/support" element={<Support />} />
<Route path="/terms" element={<Terms />} />
<Route path="/policy" element={<Policy />} />
<Route path="/about" element={<About />} />
<Route path="*" element={<Navigate to="/audit" replace />} />
</Route>
</Routes>

How can I redirect from wildcard routes to the home page?

I want to redirect from any * route to my home page, because I want to change url to '/'.
I followed this link:
React-Router: No Not Found Route?
But when I want to change any route path It moves me independently to the url '/'
<Route exact path="/" component={MMPStudio} />
<Route exact path="/galeria" component={Gallery} />
<Route exact path="/kontakt" component={Contact} />
<Route exact path="/fotobudka" component={Fotobudka} />
<Route exact path="/jubiler" component={Jubiler} />{" "}
<Route exact path="/fotobudka/kontakt" component={FotobudkaContact} />
<Route exact path="/jubiler/galeria" component={JubilerGallery} />
<Switch>
<Route exact path="/" component={MMPStudio} />
<Redirect from="*" to='/' />
</Switch>
I think your switch should wrap the whole thing:
<Switch>
<Route exact path="/" component={MMPStudio} />
<Route exact path="/galeria" component={Gallery} />
<Route exact path="/kontakt" component={Contact} />
<Route exact path="/fotobudka" component={Fotobudka} />
<Route exact path="/jubiler" component={Jubiler} />
<Route exact path="/fotobudka/kontakt" component={FotobudkaContact} />
<Route exact path="/jubiler/galeria" component={JubilerGallery} />
<Redirect from="*" to='/' />
</Switch>

React-routing config issue to change new home page

I am new to react-router.
In current app we are using below react-router
module.exports = (
<Router history={HashHistory}>
<Route path="/" component={Frame}>
<IndexRoute component={Page1} />
<Route path="all" component={Page2} />
<Route path="admin" component={Page3} />
<Route path="proxy" component={Page4} />
</Route>
</Router>
);
due to some changes now I want to make different page as home page but also want to make minimum changes to unimpact the current functionalities. Using the below router config but not working
<Router history={HashHistory}>
<Route path="/" component={Home}>
<Route path="achieve" component={Frame}>
<Route path ="achieve" component={Page1}>
<Route path="all" component={Page2} />
<Route path="admin" component={Page3} />
<Route path="proxy" component={Page4} />
</Route>
</Route>
</Route>
</Router>
Any pointer to resolve the issues and to explore this?
Thanks in advance.
If you wants to make each page as a home page then don't use nested route, make the separate route for each.
Try this:
<Router history={HashHistory}>
<Route path="/" component={Home}/>
<Route path="/achieve" component={Frame}/>
<Route path ="/achieve" component={Page1}/>
<Route path="/all" component={Page2} />
<Route path="/admin" component={Page3} />
<Route path="/proxy" component={Page4} />
</Router>
Use nesting in that route where you want to use some component inside that. Lets say you want to use some component inside admin page then :
<Router history={HashHistory}>
<Route path="/" component={Home}/>
<Route path="/achieve" component={Frame}/>
<Route path ="/achieve" component={Page1}/>
<Route path="/all" component={Page2} />
<Route path="/admin" component={Page3} >
<IndexRoute component={comp1}/>
<Route path="/xyz" component={comp2}/>
</Route>
<Route path="/proxy" component={Page4} />
</Router>
Follow these links for better explanation:
https://css-tricks.com/learning-react-router/
https://medium.com/#dabit3/beginner-s-guide-to-react-router-53094349669#.sjzpetwbb
Please comment if need any help.

React router : child routers not working

I have tried to define child routs with react router ( below code ) but child route is not working and also it doesn't returning any error message.
<Router>
<Route path="/" component={Main} >
<IndexRoute component={Dashboard} />
<Route path="create" component={UserList} />
<Route path="user/create" component={CreateUser} />
<Route path="createGroup" component={CreateGroup} />
<Route path="groups" component={GroupList} />
<Route path="premission" component={PremissionList}>
<Route path="create" component={CreatePremission} />
</Route>
</Route>
</Router>
I need to route premission/create to the create premission component.
To do multiple types of deep level/ nested routing you need to follow following approach:
<Router>
<Route path="/" component={Main} >
<IndexRoute component={Dashboard} />
<Route path="create" component={UserList} />
<Route path="user/create" component={CreateUser} />
<Route path="createGroup" component={CreateGroup} />
<Route path="groups" component={GroupList} />
</Route>
<Route path="/premission" component={PremissionList}>
<Route path="create" component={CreatePremission} />
</Route>
</Router>

Multiple master views with react-router

I can´t get multiple "master" views to work with routes when using react-router. Here is my routes config:
const routes = (
<Route path="/" component={AppView}>
<Route path="/log-in" component={LogInView}/>
<IndexRoute component={MainView}>
<IndexRoute component={HomeView} />
<Route path="company" component={CompanyView} />
<Route path="clients" component={ClientsView} />
<Route path="terms-of-service" component={TermsOfServiceView} />
<Route path="privacy-policy" component={PrivacyPolicyView} />
<Route path="contact" component={ContactView} />
</IndexRoute>
</Route>
);
Any thoughts how I can get this to work?
Thanks to knowbody the solution was quite simple, simply remove the path attribute from the top level Route element and change IndexRoute to a normal Route for the MainView:
const routes = (
<Route component={AppView}>
<Route path="/log-in" component={LogInView} />
<Route path="/" component={MainView}>
<IndexRoute component={HomeView} />
<Route path="company" component={CompanyView} />
<Route path="clients" component={ClientsView} />
<Route path="terms-of-service" component={TermsOfServiceView} />
<Route path="privacy-policy" component={PrivacyPolicyView} />
<Route path="contact" component={ContactView} />
</Route>
</Route>
);