290

Both have Route, Link, etc. When to use one or the other? I'm really confused on where to use each one. Server side? Client side?

https://reacttraining.com/react-router/

In some examples you need to pass the history, in others not. What to do?

<Router history={browserHistory}>

vs

<Router>

It's really confusing on when to use one or the other, any help appreciated.

Venryx
  • 15,624
  • 10
  • 70
  • 96
chulian
  • 3,767
  • 4
  • 21
  • 23
  • 2
    [Why is "Can someone help me?" not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) – Robert Columbia Jul 07 '18 at 01:27

5 Answers5

327

react-router contains all the common components between react-router-dom and react-router-native. When should you use one over the other? If you're on the web then react-router-dom should have everything you need as it also exports the common components you'll need. If you're using React Native, react-router-native should have everything you need for the same reason. So you'll probably never have to import anything directly from react-router. As far as when you use

<Router history={browserHistory}>

vs

<Router>

In RRv4 you won't need to pass down browserHistory, that was just for previous versions of the router.

If you're still confused, you can check out the details on each package here

Tyler McGinnis
  • 34,836
  • 16
  • 72
  • 77
  • 4
    shall we remove react-router from project or it affects react-router-dom?? is that react-router package is mandatory to work with react-router-dom? – Yogi Jun 22 '17 at 12:28
  • 9
    @snAtchnAren It's not mandatory. You should never need the "react-router" package if you've already installed "react-router-dom". – Tyler McGinnis Jun 22 '17 at 17:04
  • 4
    If I use react-router-dom over react-router, will it decrease the size of my bundle? – Vrishank May 23 '18 at 09:44
60

react-router-dom is a react-router plus:

Atombit
  • 953
  • 9
  • 12
  • The top answer here "react-router contains all the common components between react-router-dom and react-router-native." and then this answer "react-router-dom is a react-router plus:" so which is it? I'm no expert but it seems like it's still confusing for everyone. When I try Redirect with RR-dom my app fails to compile, in contradiction to the answers here. I need them both in a web-app? – Rin and Len May 26 '23 at 09:13
32

Just use react-router-dom - react-router-dom re-exports all of react-router. The link on GitHub answer (what's the diff between react-router-dom & react-router?).

Edgar
  • 6,022
  • 8
  • 33
  • 66
Aleksandr Golovatyi
  • 1,033
  • 1
  • 11
  • 18
16

In v4, react-router exports the core components and functions. react-router-dom exports DOM-aware components, like <Link> ( which renders <a>) and (which interacts with the browser's window.history ).

react-router-dom re-exports all of react-router's exports, so you only need to import from react-router-dom in your project.

(Source: GitHub)

nCardot
  • 5,992
  • 6
  • 47
  • 83
Sooraj
  • 9,717
  • 9
  • 64
  • 99
  • 1
    but do you need to npm install both react-router and react-router-dom separately? – joedotnot Nov 25 '19 at 14:43
  • @joedotnot no. react-router-dom have everything from react-router. See example here. https://reactrouter.com/web/example/basic – Imran Oct 14 '21 at 07:27
8

Found this in the Github.

Note: This package provides the core routing functionality for React Router, but you might not want to install it directly. If you are writing an application that will run in the browser, you should instead install react-router-dom. Similarly, if you are writing a React Native application, you should instead install react-router-native. Both of those will install react-router as a dependency.

Source: https://github.com/remix-run/react-router/tree/main/packages/react-router

Imran
  • 440
  • 4
  • 15