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?
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
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.
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.
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.
Use the window. location. replace() method to redirect to an external url in React, e.g. window.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With