Skip to content

Commit fc441b3

Browse files
groffcoleluisrudge
authored andcommitted
use react router history.push instead of window.history.replaceState (#154)
* use react router history.push instead of window.history.replaceState * also use history.push on the DEFAULT_REDIRECT_CALLBACK
1 parent eed6a4b commit fc441b3

8 files changed

Lines changed: 18 additions & 12 deletions

File tree

01-Login/src/App.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
2+
import { Router, Route, Switch } from "react-router-dom";
33
import { Container } from "reactstrap";
44

55
import PrivateRoute from "./components/PrivateRoute";
@@ -9,6 +9,7 @@ import Footer from "./components/Footer";
99
import Home from "./views/Home";
1010
import Profile from "./views/Profile";
1111
import { useAuth0 } from "./react-auth0-spa";
12+
import history from "./utils/history";
1213

1314
// styles
1415
import "./App.css";
@@ -25,7 +26,7 @@ const App = () => {
2526
}
2627

2728
return (
28-
<Router>
29+
<Router history={history}>
2930
<div id="app" className="d-flex flex-column h-100">
3031
<NavBar />
3132
<Container className="flex-grow-1 mt-5">

01-Login/src/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import App from "./App";
55
import * as serviceWorker from "./serviceWorker";
66
import { Auth0Provider } from "./react-auth0-spa";
77
import config from "./auth_config.json";
8+
import history from "./utils/history";
89

910
const onRedirectCallback = appState => {
10-
window.history.replaceState(
11-
{},
12-
document.title,
11+
history.push(
1312
appState && appState.targetUrl
1413
? appState.targetUrl
1514
: window.location.pathname

01-Login/src/react-auth0-spa.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React, { useState, useEffect, useContext } from "react";
22
import createAuth0Client from "@auth0/auth0-spa-js";
3+
import history from "./utils/history";
34

45
const DEFAULT_REDIRECT_CALLBACK = () =>
5-
window.history.replaceState({}, document.title, window.location.pathname);
6+
history.push(window.location.pathname);
67

78
export const Auth0Context = React.createContext();
89
export const useAuth0 = () => useContext(Auth0Context);

01-Login/src/utils/history.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { createBrowserHistory } from "history";
2+
export default createBrowserHistory();

02-Calling-an-API/src/App.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
2+
import { Router, Route, Switch } from "react-router-dom";
33
import { Container } from "reactstrap";
44

55
import PrivateRoute from "./components/PrivateRoute";
@@ -10,6 +10,7 @@ import Home from "./views/Home";
1010
import Profile from "./views/Profile";
1111
import ExternalApi from "./views/ExternalApi";
1212
import { useAuth0 } from "./react-auth0-spa";
13+
import history from "./utils/history";
1314

1415
// styles
1516
import "./App.css";
@@ -26,7 +27,7 @@ const App = () => {
2627
}
2728

2829
return (
29-
<Router>
30+
<Router history={history}>
3031
<div id="app" className="d-flex flex-column h-100">
3132
<NavBar />
3233
<Container className="flex-grow-1 mt-5">

02-Calling-an-API/src/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import App from "./App";
55
import * as serviceWorker from "./serviceWorker";
66
import { Auth0Provider } from "./react-auth0-spa";
77
import config from "./auth_config.json";
8+
import history from "./utils/history";
89

910
const onRedirectCallback = appState => {
10-
window.history.replaceState(
11-
{},
12-
document.title,
11+
history.push(
1312
appState && appState.targetUrl
1413
? appState.targetUrl
1514
: window.location.pathname

02-Calling-an-API/src/react-auth0-spa.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React, { useState, useEffect, useContext } from "react";
22
import createAuth0Client from "@auth0/auth0-spa-js";
3+
import history from "./utils/history";
34

45
const DEFAULT_REDIRECT_CALLBACK = () =>
5-
window.history.replaceState({}, document.title, window.location.pathname);
6+
history.push(window.location.pathname);
67

78
export const Auth0Context = React.createContext();
89
export const useAuth0 = () => useContext(Auth0Context);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { createBrowserHistory } from "history";
2+
export default createBrowserHistory();

0 commit comments

Comments
 (0)