42

I have a route which redirects after checking a condition like this

<Route exact path="/" render={()=>(
Store.isFirstTime ? <Redirect to="intro" /> : <Home state={Store}/>)}/>

The url changes when the condition is true but the component is not mounted. The rest of the component code is as below.

render() {
    return (
      <div>
        ...
        <Route exact path="/" render={()=>(
          Store.isFirstTime ? <Redirect to="intro" /> : <Home state={Store} />         
        )} />
        <Route path="/intro" render={()=>(<IntroWizard state={Store.userInfo}/>)} />
        <Route path="/home" render={()=>(<Home state={Store}/>)} />
        <Route render={()=>(<h1>404 Not Found</h1>)} />
        <Footer />
      </div>
    );
  }

My App Component is contained with in the BrowserRouter like thi

ReactDOM.render(<BrowserRouter>
    <App/>
</BrowserRouter>,
  document.getElementById('root')
);

when I hit the url directly in the browser like 'localhost:3000/intro' component is mounted successfully, but when it goes through the redirect it doesn't display the component. How do I fix it?

Edit

So one detail was missing and I tried creating another project to reproduce the issue. My App component is a observer from mobx-react and it is exported as shown below

let App = observer(class App { ... })
export default App

I have created this repo with a sample code to reproduce the issue you can use it https://github.com/mdanishs/mobxtest/

So when Components are wrapped into mobx-react observer the redirect is not working else it works fine

2
  • Do you see any errors in the console? Commented Mar 19, 2017 at 2:52
  • @TharakaWijebandara no errors in the console, infact I just tried using a link, it is not working too. No idea what is going wrong.
    – mdanishs
    Commented Mar 19, 2017 at 11:40

9 Answers 9

53

The asker posted an issue on GitHub, and got this apparently unpublished hidden guide (edit: now published) that helped me out too. I'm posting it here because I ran into the same problem and want others to avoid our pain.

The problem is that mobx-react and react-redux both supply their own shouldComponentUpdate() functions that only check for prop changes, but react-router sends state down through the context. When the location changes, it doesn't change any props, so it doesn't trigger an update.

The way around this is to pass the location down as a prop. The above guide lists several ways to do that, but the easiest is to just wrap the container with the withRouter() higher order component:

export default withRouter(observer(MyComponent))

or, for redux:

export default withRouter(connect(mapStateToProps)(MyComponent))
1
  • Yes generally React Router and Redux work fine together. Occasionally though, an app can have a component that doesn't update when the location changes (child routes or active nav links don't update). This is called Blocked Updates. The problem is that Redux implements shouldComponentUpdate and there's no indication that anything has changed if it isn't receiving props from the router. This is straightforward to fix. Find where you connect your component & wrap it in withRouter. github.com/ReactTraining/react-router/blob/master/packages/… Commented Jan 23, 2019 at 16:51
26

You should render only Redirect in your render method:

render() {
  return (<Redirect to="/" />);
}

but here is my favorite solution without using the Redirect component

  1. import import { withRouter } from 'react-router-dom';
  2. export default withRouter(MyComponent);
  3. Finally, in your action method, assume that handlingButtonClick
handlingButtonClick = () => {
  this.props.history.push("/") //doing redirect here.
}
3
  • 1
    It doesn't solves all cases. I've already tried both but it doesn't work. All works for the FIRST Redirect only, but if I return/redirect/history.push() at the first page and then try to do the SAME redirect as before, it seems refresh the DOM/page but I'm stucked there.
    – emandt
    Commented Aug 23, 2019 at 14:48
  • 1
    this.props.history.push('/something') was the onlything that worked for me
    – blah blah
    Commented May 9, 2020 at 19:27
  • me too, only works using withRouter and this.props.history.push thanks bro Commented May 31, 2020 at 18:00
22

<Redirect /> should not be inside <Switch></Switch> tags.

3
  • 9
    Lol, of all the over-engineered solutions on this thread, this is the only that worked for me, and it's a one liner.
    – Ash
    Commented Jun 1, 2020 at 8:16
  • Tried it. Didn't help in my case. Commented Jan 28, 2021 at 6:56
  • 1
    This solved my problem! After placing <Redirect> out of the <Switch>, component renders correctly. Thanks!
    – Casana
    Commented Mar 30, 2021 at 10:14
6

Be careful when composing the Router with other components like translation providers, theme providers, etc .. After some time I found that your App has to be strictly wrapped by the Router, like this:

<Provider store={store}>
  <ThemeProvider theme={theme}>
    <TranslationProvider
      namespaces={['Common', 'Rental']}
      translations={translations}
      defaultNS={'Common'}
    >
      <Router>
        <App />
      </Router>
    </TranslationProvider>
  </ThemeProvider>
</Provider>

Hope this helps somebody

2
  • Thank you very much, you helped me a lot! In my case IntlProvider was the culprit. I think it might be because of context or some shouldComponentUpdate magic.
    – dan-lee
    Commented Aug 15, 2018 at 16:38
  • Great, this was the issue for me as well, had wrapped my routes in ThemeProvider which caused the issue.
    – LanfeaR
    Commented Nov 4, 2018 at 10:58
3

You're missing a / in front of intro

<Route exact path="/" render={()=>(
  Store.isFirstTime ? <Redirect to="/intro" /> : <Home state={Store} />         
)} />
2
2

you can try this way like:

first: import "withRouter" to component like this:

import { withRouter } from "react-router-dom";

then at the end of your component export compoenent like this:

export default withRouter(VerifyCode);

and then you can add this code to your function:

 verify(e) {
    // after sever get 200
    this.props.history.push('/me/');
}

good luck

2

Add "exact" in order to redirect 👍

Example :

<Switch>
   <Route exact path="/">
      <Redirect to="/home" />
   </Route>
<Switch>
0
  const App = ({history}) =>{

  return history.push('/location')  }
0

I have researched for 3 hours with ReactJS redirect, and here the best method to redirect, i created a module like this:

import { useHistory, withRouter } from "react-router-dom";

const Redirect = (props: any) => { 
    const to = (props.to || '/');
    let history = useHistory();
        history.push(to);
        window.location.reload();
    return (
      <>
      </>
     )
   }
export default withRouter(Redirect);

ANd it work perfect! You can use in other file! Like this:

    export default function RedirectExample() {
      return (
        <Router>
          <Switch>
            <Route path="/page/2">
              <QueryPage />
            </Route>
            <Route path="/page">
              <MyRedirect to="/" />
            </Route>
          </Switch>
        </Router>
      );
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.