I was looking for the simplest way to render the same ponent but from different paths.
I have the following so that both "/"
and "/login"
render the Login ponent.
import React from "react";
import { Route, Switch, Redirect } from 'react-router-dom';
import './App.scss';
import Login from "../../login/Login";
const App = () => {
return (
<div className="App">
<div className="App-container">
<Switch>
<Route exact path={["/", "/login"]} ponent={() =>
<Login login={true} />}/>
<Redirect to="/" />
</Switch>
</div>
</div>
);
}
export default App;
This does appear to work, however, it returns an error in the console.
Warning: Failed prop type: Invalid prop 'path' of type 'array' supplied to 'Route', expected 'string'.
I'm trying to do this...
<Route exact path={"/"} ponent={() => <Login login={true} />}/>
<Route exact path={"/login"} ponent={() => <Login login={true} />}/>
But with a shorter method, is this possible with react-router-dom
? Any help would be greatly appreciated
I was looking for the simplest way to render the same ponent but from different paths.
I have the following so that both "/"
and "/login"
render the Login ponent.
import React from "react";
import { Route, Switch, Redirect } from 'react-router-dom';
import './App.scss';
import Login from "../../login/Login";
const App = () => {
return (
<div className="App">
<div className="App-container">
<Switch>
<Route exact path={["/", "/login"]} ponent={() =>
<Login login={true} />}/>
<Redirect to="/" />
</Switch>
</div>
</div>
);
}
export default App;
This does appear to work, however, it returns an error in the console.
Warning: Failed prop type: Invalid prop 'path' of type 'array' supplied to 'Route', expected 'string'.
I'm trying to do this...
<Route exact path={"/"} ponent={() => <Login login={true} />}/>
<Route exact path={"/login"} ponent={() => <Login login={true} />}/>
But with a shorter method, is this possible with react-router-dom
? Any help would be greatly appreciated
- 1 Possible duplicate of Multiple path names for a same ponent in React Router – Arnaud Christ Commented Mar 12, 2019 at 8:59
-
@ArnaudChrist That is for
react-router
. The solution in that question is what I am using above, which is giving me an error in the console. – mcclosa Commented Mar 12, 2019 at 9:00 -
Did you try with the answer using regular expression string from Cameron in the mentioned post ?
path="/(home|users|widgets)/"
– Arnaud Christ Commented Mar 12, 2019 at 9:03 -
Yes, just wasn't sure how to target
/
with regular expression? – mcclosa Commented Mar 12, 2019 at 13:31
2 Answers
Reset to default 3You could create an array that contains the paths /
and /login
and use map
on that array to render the same thing for both paths.
<Switch>
{["/", "/login"].map(path => (
<Route
key={path}
exact
path={path}
render={() => <Login login={true} />}
/>
))}
<Redirect to="/" />
</Switch>
If you wish to render the same ponent on the several routes, you can do this by specifying your path as a regular expression
lets say you want to display 'Home' ponent for 'Home', 'User' and 'Contact' ponents then here is code.
<Route path="/(home|users|contact)/" ponent={Home} />