admin管理员组文章数量:1399234
I'm building a web app with three main parts: The actual website, an admin part and a user part. For each part, I want a different layout wrapper.
This is my code right now:
Website wrapper
const Website = () => (
<React.Fragment>
<NavigationBar />
<main>
<div className="container">
<Switch>
<Route exact path='/' ponent={Home}/>
<Route exact path='/login' ponent={Login}/>
</Switch>
</div>
</main>
<FooterBar />
</React.Fragment>
);
User wrapper
const User = () => (
<React.Fragment>
<UserSideBar />
<main>
<div className="container">
<Switch>
<Route exact path='/u/dashboard' ponent={UDashboard}/>
<Route exact path='/u/account' ponent={UAccount}/>
</Switch>
</div>
</main>
</React.Fragment>
);
Admin wrapper
const Admin = () => (
<React.Fragment>
<main>
<div className="container">
<Switch>
<Route exact path='/a/dashboard' ponent={ADashboard}/>
<Route exact path='/a/payments' ponent={APayments}/>
<Route exact path='/a/account' ponent={AAccount}/>
</Switch>
</div>
</main>
</React.Fragment>
);
Router
const Router = () => (
<BrowserRouter>
<Switch>
<Route path="/u" ponent={User} />
<Route path="/a" ponent={Admin} />
<Route path="/" ponent={Website} />
<Redirect from="*" to="/" />
</Switch>
</BrowserRouter>
);
It's all working fine but if an url is not matching any of the routes, it's not redirecting to '/'
. How can I achieve this?
I'm building a web app with three main parts: The actual website, an admin part and a user part. For each part, I want a different layout wrapper.
This is my code right now:
Website wrapper
const Website = () => (
<React.Fragment>
<NavigationBar />
<main>
<div className="container">
<Switch>
<Route exact path='/' ponent={Home}/>
<Route exact path='/login' ponent={Login}/>
</Switch>
</div>
</main>
<FooterBar />
</React.Fragment>
);
User wrapper
const User = () => (
<React.Fragment>
<UserSideBar />
<main>
<div className="container">
<Switch>
<Route exact path='/u/dashboard' ponent={UDashboard}/>
<Route exact path='/u/account' ponent={UAccount}/>
</Switch>
</div>
</main>
</React.Fragment>
);
Admin wrapper
const Admin = () => (
<React.Fragment>
<main>
<div className="container">
<Switch>
<Route exact path='/a/dashboard' ponent={ADashboard}/>
<Route exact path='/a/payments' ponent={APayments}/>
<Route exact path='/a/account' ponent={AAccount}/>
</Switch>
</div>
</main>
</React.Fragment>
);
Router
const Router = () => (
<BrowserRouter>
<Switch>
<Route path="/u" ponent={User} />
<Route path="/a" ponent={Admin} />
<Route path="/" ponent={Website} />
<Redirect from="*" to="/" />
</Switch>
</BrowserRouter>
);
It's all working fine but if an url is not matching any of the routes, it's not redirecting to '/'
. How can I achieve this?
-
You can add the
Redirect
logic in yourWebsite
ponent but this time/u/foo
or/a/bar
does not work. I can't find a solution this is why I am going to follow this question :) – devserkan Commented Aug 16, 2018 at 23:48
1 Answer
Reset to default 6You could just make a reusable ponent NoMatch and included at the bottom of your Switch
.
For example:
<Switch>
<Route exact path='/a/dashboard' ponent={ADashboard}/>
<Route exact path='/a/payments' ponent={APayments}/>
<Route exact path='/a/account' ponent={AAccount}/>
<Route ponent={Notfound} />
</Switch>
Make sure to put it at the very end So, if no other route was matched, that one would be displayed.
Or you can make a simple custom function which you can reuse depending on your needs:
You could do this if the undefined path is not clear:
const createRedirect = to => () => <Redirect to={to} />
// ...
<Route path="/*" ponent={createRedirect('/foo')} />
To implement a redirect
fallback in a switch
, you need to do:
<Switch>
<Route path="/foo" ponent={Bar}/>
<Route path="/bar" ponent={Baz}/>
<Route render={() => <Redirect to="/foo"/>}/>
</Switch>
Or you can take the longer approach and make it programmatically redirect. Here is a reference of a reusable ponent, or at least reusable logic that you could implement to your needs: Reference
本文标签: javascriptReact router nested routesHow to redirect when no route is matchingStack Overflow
版权声明:本文标题:javascript - React router nested routes - How to redirect when no route is matching - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744191352a2594531.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论