Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Router v4 Redirect not working

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

like image 351
mdanishs Avatar asked Mar 18 '17 15:03

mdanishs


People also ask

Why is redirect not working react router Dom?

To solve the error "export 'Redirect' (imported as 'Redirect') was not found in 'react-router-dom'", use the Navigate component instead of Redirect , e.g. <Navigate to="/dashboard" replace={true} /> . The Navigate component changes the current location when it's rendered.

Why is redirect not working?

A URL may not be redirecting for many reasons: Browser cache – Your browser has cached a previous request. Clear your browser cache to get the latest. Server cache – Your site is being cached with some caching software, and this needs to be updated to include your redirect.

How do I redirect on a react router?

The Redirect component was usually used in previous versions of the react-router-dom package to quickly do redirects by simply importing the component from react-router-dom and then making use of the component by providing the to prop, passing the page you desire to redirect to.

How do I redirect a URL in react JS?

Use the window. location. replace() method to redirect to an external url in React, e.g. window.


1 Answers

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)) 
like image 108
neurozero Avatar answered Oct 15 '22 10:10

neurozero