Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React history.push requiring a forceRefresh

I have implemented createBrowserHisotry according to this post How to get history on react-router v4?, however it only redirects if I set a configuration property to forceRefresh: true. If I do not set any config properties, the url changes to the pushed url but the page will not redirect. Why?

INDEX.JS

import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./components/App";
import { Router } from 'react-router-dom'
import registerServiceWorker from "./registerServiceWorker";
import history from "./components/common/History";

ReactDOM.render(<Router history={history}><App /></Router>, 
document.getElementById('root'));
registerServiceWorker();

HISTORY.JS

import createBrowserHistory from 'history/createBrowserHistory';

// export default createBrowserHistory(); --> This redirects the ### ### URL 
// but does not physically redirect

export default createBrowserHistory({
//pass a configuration object here if needed
forceRefresh: true
});

APP.JS

class App extends Component {

  render() {
    return (
      <Provider store={store}>
        <OidcProvider store={store} userManager={userManager}>
         <Router>
           <div className="container-fluid">
            <div>
             <Header />
            </div>
           <div>
            <Route exact path="/" render={props => <LoginForm auth={this.auth} {...props} />} />
            <Route exact path="/callback" render={props => <Callback auth={this.auth} {...props} />} />
            <Route exact path="/home" render={props => <HomePage {...props} />} />
            <Route exact path="/table" render={props => <TableContainer {...props} />} />
            <Route exact path="/form/:id" render={props => <FormContainer id={this.props.params.id} {...props} />} />
            <Route exact path="/form" render={props => <FormContainer {...props} />} />
          </div>
          <Footer />
        </div>
      </Router>
    </OidcProvider>
  </Provider>
);
}
}

 export default App;

Another file calling history.push

import history from "../components/common/History";

class CallbackPage extends React.Component {
render() {
    // just redirect to '/' in both cases
    return (
        <CallbackComponent
            userManager={userManager}
            successCallback={() => history.push("/home")}
            errorCallback={error => {
                history.push("/");
                console.error(error);
            }}
        >
            <div>Redirecting...</div>
        </CallbackComponent>
    );
   }
}


export default connect()(CallbackPage);
like image 772
reactFullStackDeveloper Avatar asked Nov 28 '22 21:11

reactFullStackDeveloper


1 Answers

Use this:

this.props.history.push('/index');    
this.props.history.go();
like image 94
Genildo Avatar answered Dec 05 '22 05:12

Genildo