reactjsreact-routerbrowser-historyhtml5-historyhistory.js

React router history.push falls back to 404 route


Everytime I execute history.push("/path") the url changes to the correct path but the 404 PageNotFound component gets renderered.

// indes.tsx
import React from 'react';
import ReactDOM from 'react-dom';
import {App} from './App';
import history from "./customHistory";
import {Router} from "react-router";

ReactDOM.render(
    <React.StrictMode>
        <Router history={history}>
           <App/>
        </Router>
    </React.StrictMode>,
    document.getElementById('root')
);
/// App.tsx
import React, {Component} from "react"
import {Route, Switch} from "react-router-dom";
import {FrontPage} from './components/FrontPage'
import {LoginPage} from "./components/LoginPage";
import {PageNotFound} from "./components/PageNotFound";
import {RequestPasswordResetPage} from "./components/RequestPasswordResetPage";

export class App extends Component {

    render() {
        return (
            <Switch>
                <Route path={"/"} component={FrontPage} exact={true}/>

                <Route path={"/login"} component={LoginPage} exact={true}/>

                <Route path={"/request_password_reset"} component={RequestPasswordResetPage}
                       exact={true}/>

                <Route path={""} component={PageNotFound}/>
            </Switch>
        )
    }
}

My history object is the following

// customHistory.ts
import { createBrowserHistory } from "history";

export default createBrowserHistory({});

And I call the history.push after the user requested a password reset:

// RequestPasswordResetPage.tsx
private handleSubmit(event: FormEvent<HTMLFormElement>) {
        event.preventDefault();

        AccountService.requestPasswordReset(this.state.email)
            .then((ans: Response) => {
                if (ans.ok) {
                    console.log("REDIRECT TO HOME");
                    history.push("/login");
                } else {
                    throw Error(ans.statusText);
                }
            });
    }

Everytime the url changes to localhost:3000/login but the PageNotFound component gets rendered.


Solution

  • Use react-router v5.2.0 and history v4.9.0 to make this work.

    https://codesandbox.io/s/quizzical-dan-z3725

    OR

    Try using BrowserHistory https://reactrouter.com/web/example/basic

    seems there is some issue with customHistory when we use different version