-
Notifications
You must be signed in to change notification settings - Fork 137
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
Routes with Redirects in them render twice #55
Comments
Additional evidence for #54 Closing so that we can concentrate the bug tracking. |
#54 is now closed but I don't see how I can apply the fixes in that issue to this one. Any ideas? |
@AustP i should have some time to look at this over the next couple days. |
@AustP i haven't had the chance to try to repro this yet, but i imagine that the problem is from using my thinking this is because if my hunch is true, removing the export default class Dashboard extends Component {
// this probably belongs in some hoc or something
componentDidMount () {
if (!isAuthenticated) {
this.props.history.push('/');
}
}
render() {
return isAuthenticated
? <div>Hi hello</div>
: null;
}
} <Switch key={location.key} location={location}>
<Route exact path="/" render={() => <div>Please log in</div>}/>
<Route exact path="/dashboard" component={Dashboard} />
</Switch> if you could, give that a shot and let me know if it works. |
@AustP not sure if you got this resolved or not. that said, I just published a new version of this library last night which greatly simplifies the API. it might be worth looking into? |
@maisano |
Same here. Trying to Redirect programatically but due to the double render it starts a loop that crashes the app. |
@Plorark can you paste what the code looks like (specifically the |
@maisano Something like this in switch declaration:
And like this in component
And then I just change the The error I get are thousands of: It also happens with: |
So...... |
Instantiate your own history object and pass it to Router. NOTE: you cannot use BrowserRouter you will need to use Router. // history.js
import createHistory from 'history/createBrowserHistory';
export default createHistory(); // index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route } from 'react-router-dom';
import App from './App';
import history from './history';
ReactDOM.render(
<Router history={history}>
<Route path="/" component={App} />
</Router>
, document.getElementById('root')); Now you can use // test.js
import history from './history';
const pushItRealGood = () => history.push('/home'); You can even use it in your Redux actionCreators if you like // actions.js
import history from './history';
export const forgotPassword = data => (dispatch, getState, api) =>
api.post('/request-password-reset', data)
.then(() => {
dispatch({ type: "SOMETHING_HERE", payload });
history.push('/forgot-password?submission=success');
}).catch(error => error);
export const resetPassword = (data, token) => (dispatch, getState, api) =>
api.post(`/users/reset-password?access_token=${token}`, data)
.then(() => {
dispatch({ type: "SOMETHING_HERE", payload });
history.push({
pathname: '/signin',
state: {
message: 'Successfully updated your password. Please login to continue.'
}
});
}).catch(error => error); |
This code generates a warning.js?d96e:36 Warning: You tried to redirect to the same route warning:
|
I have the same warning as @Lapico. Any news about it? |
I have a component that checks user authorization and redirects them if they are unauthorized. It redirects them using the
Redirect
component fromreact-router-dom
.When I wrapped my routes in a
RouteTransition
component and passedlocation
andlocation.key
to theSwitch
component, I noticed that whenever my app would redirect someone, the render logic would execute twice.If you spin up a server with the following code and visit
/dashboard
, you will see that "wot" gets logged to the console twice before being redirected to/
.What do I need to do to make my render logic only execute once?
The text was updated successfully, but these errors were encountered: