最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Why on react history createHashHistory appends # for each path? - Stack Overflow

matteradmin6PV0评论

I have an application with this config for history:

import { createHashHistory } from 'history';
import { ConnectedRouter } from 'connected-react-router';

const history = createHashHistory({
  hashType: 'slash',
});
    ...
  <ConnectedRouter history={history}>
    <App />
  </ConnectedRouter>

But all my routes get appended by /# ex: localhost:8080/ bees: localhost:8080/#/

I already tried to update my packages as this question say but it didn't work.

The only thing that worked was change createHashHistory to createBrowserHistory, but I'm not sure what's the difference between them, and why createHashHistory is appending the /#

I have an application with this config for history:

import { createHashHistory } from 'history';
import { ConnectedRouter } from 'connected-react-router';

const history = createHashHistory({
  hashType: 'slash',
});
    ...
  <ConnectedRouter history={history}>
    <App />
  </ConnectedRouter>

But all my routes get appended by /# ex: localhost:8080/ bees: localhost:8080/#/

I already tried to update my packages as this question say but it didn't work.

The only thing that worked was change createHashHistory to createBrowserHistory, but I'm not sure what's the difference between them, and why createHashHistory is appending the /#

Share Improve this question asked May 28, 2020 at 20:41 Lucas AndradeLucas Andrade 4,6185 gold badges33 silver badges53 bronze badges 1
  • 1 Hash routing in general is UI routing that contains a /#/. It utilizes the hashchange and window.location.hash API to simulate routing. Alternately, UI routing uses the history API to update the URL and store previous states in browser memory. – Austin Ezell Commented May 28, 2020 at 20:54
Add a ment  | 

1 Answer 1

Reset to default 4

With hashHistory, it produces url like http://yourwebsite/#page/xxx

With browserHistory, it produces url like http://yourwebsite/page/xxx

Which one to use? In real-world products, browserHistory is mostly used. A rule of thumb is "if you are using a dynamic server that can handle dynamic URLs then you need to use the BrowserRouter ponent but if you are using a server that only serves static files then a HashRouter ponent is what to be used in this case."

In your code, hashType: 'slash' is just the default value.

Post a comment

comment list (0)

  1. No comments so far