71

I've read many things about react-router v4 and the npm history library to this point, but none seems to be helping me.

My code is functioning as expected up to the point when it should navigate and do a simple redirect when the url is changed using the history.push() method. The URL IS changing to the specified route, but not doing the redirect on the button push. Thanks in advance, still learning react-router...

I would like for the button push to do a simple redirect without the {forceRefresh:true}, which then reloads the whole page.

import React from 'react';
import createBrowserHistory from 'history/createBrowserHistory';

const history = createBrowserHistory({forceRefresh:true});

export default class Link extends React.Component {
  onLogout() {
    history.push("/signup");
  }
  render() {
      return(
        <div>
          <h1>Your Links</h1>
          <button onClick={this.onLogout.bind(this)}>Log Out</button>
        </div>
      )
  }
}
John
  • 885
  • 1
  • 6
  • 10

6 Answers6

63

You shouldn't need to downgrade to v3, React-Router 4.0.0 is totally capable of accomplishing what the OP asks for.

const history = createBrowserHistory();

is a custom history object so you should use <Router> to synchronize it with react-router instead of <BrowserRouter>, which is what I assumed you were using.

Try this instead:

import React, {Component} from 'react';
import { Router } from 'react-router';
import { Route } from 'react-router-dom';
import createHistory from 'history/createBrowserHistory';

const history = createHistory();   

class App extends Component {
   constructor(props){
      super(props);
   }

   render(){
       return (
           <Router history={history}>   //pass in your custom history object
                <Route exact path="/" component={Home} />
                <Route path="/other" component={Other} />
           <Router />
       )
   }
}

Once your custom history object is passed in via Router's history prop, history.push should work just as expected in anywhere of your app. (you might want to put your history object in a history config file and import it at places where you want to route programmatically).

For more info, see: React Router history object

halfer
  • 18,701
  • 13
  • 79
  • 158
Tina Chen
  • 819
  • 1
  • 7
  • 13
  • 11
    Wow thank you! This is the only answer I've seen that explains that you need to use `` instead of `` when using the history package. – Chase DeAnda Jul 18 '17 at 15:38
  • 3
    This is the most correct solution I found in my search of last 4 days – Abhi Aug 06 '17 at 07:11
  • 2
    Many thanks, history now works with but basename stopped working in it. In basename works but history not working :-( – Jay Nov 07 '19 at 16:04
  • 1
    Thanks a lot man. It wasnt working with BrowserRouter but after changing to Router and adding my history as param it worked like a charm – ncesar Mar 18 '21 at 14:22
41

Check if you don't have nested BrowserRouter tags.

I had this issue on react-router v4 but I solved it after changing the app to only have the BrowserRouter at the top most level like the example below.

ReactDOM.render(
  <BrowserRouter >
    <App />
  </BrowserRouter>,
  document.getElementById('root')
);
Artur Carvalho
  • 6,046
  • 10
  • 62
  • 91
6

Rendering this would refresh on route change

 `ReactDOM.render(
    <AppContainer>
      <BrowserRouter children={routes} basename={baseUrl} forceRefresh={true}/>
    </AppContainer>,
    document.getElementById("react-app")
  );`

react-router-dom : 4.2.2

bathi d
  • 61
  • 1
  • 1
6

Please ensure that you are using

const history = createBrowserHistory({forceRefresh:true});

not

const history = createBrowserHistory();

I was able to update the page once I added "{forceRefresh:true}"

Anupam Chaplot
  • 508
  • 4
  • 16
5

import Router from react-router-dom not BrowserRouter

// index.js

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


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

    serviceWorker.register();


// history.js 

import { createBrowserHistory } from 'history';

export default createBrowserHistory();

on other components that you need to navigate import history and push

import History from '../utils/history'

History.push('/home')

C.K
  • 1,742
  • 1
  • 13
  • 29
0

I have the same problem.

After some search i have found this react-router issue

I have currently downgrade react-router to the v3 and i use browserHistory from the react-router package and it's work fine.

ndufreche
  • 107
  • 6