Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hashHistory stopped working in 4.0.0 #4752

Closed
ehartford opened this issue Mar 14, 2017 · 9 comments
Closed

hashHistory stopped working in 4.0.0 #4752

ehartford opened this issue Mar 14, 2017 · 9 comments

Comments

@ehartford
Copy link

ehartford commented Mar 14, 2017

Version

4.0.0

Test Case

the jsbin doesn't work because the library is too old, need 4.0.0

Steps to reproduce

Upgrade to 4.0.0

import { Router, hashHistory } from 'react-router';
render(<Router history={hashHistory}>{routes}</Router>, root);

Expected Behavior

It should work

Actual Behavior

It doesn't work

Warning: Failed prop type: The prop `history` is marked as required in `Router`, but its value is `undefined`.
in Router
printWarning
localhost/:36 Uncaught TypeError: Cannot read property 'location' of undefined
at new Router (eval at <anonymous> 
@emjaksa
Copy link

emjaksa commented Mar 14, 2017

It has been moved to a new package, react-router-dom. Install the new package and follow the docs for HashRouter

@ickata
Copy link

ickata commented Mar 14, 2017

It's not clear how to use it from the docs. How to migrate this code for example:

ReactDOM.render((
   <Router history={ReactRouter.hashHistory}>
      <Route path="/" component={Login} />
      <Route path="dashboard" component={Dashboard} />
      <Route path="users" component={Users} />
   </Router>
), document.getElementById( 'page' ) )

@emjaksa
Copy link

emjaksa commented Mar 14, 2017

The getting started guide might be more clear. Just replace BrowserRouter with HashRouter. You no longer need to pass the history as a prop anymore. So for your code it would be.

import React from 'react';
import ReactDOM from 'react-dom';
import {
  HashRouter,
  Route,
  Link
} from 'react-router-dom';

ReactDOM.render((
   <HashRouter>
      <div>
        <Route exact path="/" component={Login} />
        <Route path="dashboard" component={Dashboard} />
        <Route path="users" component={Users} />
      </div>
   </HashRouter >
), document.getElementById( 'page' ) )

@orangecoding
Copy link

@timdorr question on this one: we are currently upgrading from R-router 2 to 4. In 2, we had a file where we used require('react-router').hashHistory; to listen to the hashHistory (also used to push location changes)

How can I do this with the new component HashRouter?

@pshrmn
Copy link
Contributor

pshrmn commented Jun 19, 2017

@orangecoding https://github.com/ReactTraining/react-router/blob/master/FAQ.md#how-do-i-access-the-history-object-outside-of-components

@elquimista
Copy link

@orangecoding you can still listen to history in react-router 4.

class App extends React.Component {
  componentDidMount() {
    const { history } = this.props;
    history.listen(e => {
      console.log('listen', e.pathname);
    });
  }
  render() {
    ...
  }
}

...

ReactDOM.render(
  <Provider store={store}>
    <Router>
      <Route component={App} />
    </Router>
  </Provider>,
  document.getElementById('root')
);

@Anna098301065
Copy link

'react-router' does not contain an export named 'hashHistory'. why?

@timdorr
Copy link
Member

timdorr commented Aug 10, 2017

@Anna098301065 The API in 4.0 was completely changed. Please read through the docs.

@Hiroki111
Copy link

Hiroki111 commented Aug 20, 2017

`import React from 'react';
 import {
     HashRouter,
         Route,
     NavLink
 }
 from 'react-router-dom';
 import Posts from './Posts';

 export default class App extends React.Component {
    constructor(props) {
	    super(props);
    }

    render() {
	return (
		<HashRouter>
			<div>
				<ul className="sidebar">
    				<li className="sidebar-list">
    					<NavLink to="/posts" >Posts</NavLink>
    				</li>
    			</ul>
				<div className="admin-contents">
					<Route path="/posts" component={Posts}/>
				</div>
			</div>
		</HashRouter>
	);
}

}`

Accordting to emjaksa, using HashRouter would solve the error.

In my case, however, clicking Link (or NavLink) shows an error message
"Warning: Hash history cannot PUSH the same path; a new entry will not be added to the history stack".
(although the page itself is properly renderted)

AndresMartinMaq referenced this issue in tylermcginnis/Redux Sep 11, 2017
@lock lock bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants