-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not triggering Redirect with multiple route matches #5155
Comments
As a followup, a minimal demo of the issue:
Even though when navigating from |
I believe that this is a result of some optimalization on either React or react-router side - in this scenario the A solution to this error is adding
to the Redirect component ( https://github.com/ReactTraining/react-router/blob/master/packages/react-router/modules/Redirect.js ). Not sure if this use-cause is expectd and if such a solution is accepted. |
maybe rewrite it like this?: <Route exact path="/" component={() => <p>Home</p>} />
<Route path="/" component={Authentication} /> --> <Route path="/" component={Authentication} /> const Authentication = () => isLoggedIn ? <Home/> : <Redirect to="/login" />; |
my theoretical suggest: const Home = (params) => <p>Home</p>;
// hoc
const Auth = Component => ({isLoggedIn}) => isLoggedIn ? Component : <Redirect to="/login" />;
const AuthorizedHome = Auth(<Home/>);
const ConnectedAuthrizedHome = connect(store => ({isLoggedIn: store.isLoggedIn}))(AuthorizedHome);
<Route path="/" component={ConnectedAuthrizedHome} /> |
I will have a try and seems to be a valid solution from an architectural view. Does not solve the issue with react router itself though :) |
This is related to #5162/#5003. I'm going to close this so that discussions of the redirect issue can be concentrated to over there. I'm not really a fan of the route layout that you are using and I think some simple changes would help you avoid the redirect issue. The approach that I would take is to:
const NormalRoutes = () => (
<Switch>
<Route exact path="/" component={Home} />
<Route path="/person/:id" component={Person} />
</Switch>
)
const ForceAuthRoutes = () => (
<Switch>
<Route path="/login" component={Login} />
<Redirect to='/login' />
</Switch>
)
const Routes = props => props.activeUser.jwt ? <NormalRoutes /> : <ForceAuthRoutes /> |
Version
4.1.1
Test Case
https://github.com/milanvdmria/MyFirstReactProject (npm start)
https://github.com/milanvdmria/MyFirstReactProject/blob/master/src/components/Authentication.jsx
https://github.com/milanvdmria/MyFirstReactProject/blob/master/src/scenes/app/routes.jsx
simplified: https://codepen.io/anon/pen/YVROMK (not known with codepen)
Steps to reproduce
When loading a page, the Authentication component is triggered. When you are not logged in, it does a
<Redirect to='/login' />
.Everything works when going to the pages. When you are on a page, and you click on the Home icon in the left top corner, (a Link component to '/') it triggers all the needed components (including the Authentication) but it just renders the Home page without redirecting to the /login page.
Expected Behavior
When clicking the Home button, it links to the home page and should trigger the Authentication and redirect to the login page
Actual Behavior
It goes to the Home page, although Authentication returns a component.
The text was updated successfully, but these errors were encountered: