History push doesn't render navigated page
See original GitHub issueI got the following Router. I want to navigate to a new page. For this I tried <Redirect> first and this.props.history.push("/login") later.
In both cases the URL gets updated in the browser, but the page doesn’t rerender.
I’m using redux, redux-actions, redux-saga, react-router, react-router-dom, react, redux-form, react-dom
Example component
export namespace Hom {
export interface Props extends RouteComponentProps<void> {
authenticated: boolean;
}
export interface State {}
}
//@connect(mapStateToProps, mapDispatchToProps)
class Hom extends React.Component<any, any> {
// this function is bound
goLogin = () => {
return (
<Redirect to="/login" />
)
}
goLogin2 = () => {
this.context.router.transition('/login');
}
componentWillMount() {
if (!this.props.authenticated) {
this.props.history.push('/login');
}
}
render() {
const { children, authenticated } = this.props;
return (
<div id="home">
{ authenticated && <div>
<Header />
<Row>
<News />
<LastSymbols />
</Row>
</div> }
</div>
);
}
}
function mapStateToProps(state: RootState) {
return {
authenticated: state.auth.authenticated
};
}
function mapDispatchToProps(dispatch) {
return {};
}
export const Home = withRouter(connect<any, any, any>(mapStateToProps, mapDispatchToProps)(Hom))
ReactDOM.render(
<Provider store={store}>
<BrowserRouter>
<App>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/home" component={Home} />
<Route path="/stock/:name" component={Stock} />
<Route path="/login" component={Login} />
</Switch>
</App>
</BrowserRouter>
</Provider>,
document.getElementById('root')
);
Version
newest
Steps to reproduce
apply code above
Expected Behavior
render new page
Actual Behavior
doesn’t render new page
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:6
Top Results From Across the Web
Component is not getting rendered after history.push()
You'll import this history instance and navigate with history.push('/'); and so on. Working example: https://codesandbox.io/s/5ymj657k1k.
Read more >history.push not rendering component in Router #7415 - GitHub
I experienced same issue now. The URL changed to the supposed history.push("/myURL"), however, the page is not rendering the component. Any ...
Read more >History - React Router: Declarative Routing for React.js
The history object is mutable. Therefore it is recommended to access the location from the render props of <Route> , not from history.location...
Read more >Migrating to React Router v6: A complete guide
Migrate your React Router applications from v5 to v6 with this in-depth guide, including a review of additions and improvements from v5.
Read more >Programmatically Navigate with React Router - Telerik
history.push() is another approach where we make use of the history props React Router provides while rendering a component.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

solution: removed `@connect(mapStateToProps, mapDispatchToProps) on App
@gr8pathik Nope. I believe I just did something else and avoided having to do this. Please post your solution here if you find it. Sorry, and thanks