Create-react-app: How to add something like react-router?

Created on 26 Jul 2016  Â·  4Comments  Â·  Source: facebook/create-react-app

Hi guys,

thanks for the very well done quick-start package!
I'm not a frontend dev, so forgive me if I'm asking for something really basic.
As far as I understand webpack, you have to add dependencies (like react-router) in webpack.config.js.
Can I do this without ejecting?

Most helpful comment

Not quite, to add react-router you have to:

  1. Install it via npm install --save react-router
  2. Use react-router in your app!

For example, you could do something like this:

import { Router, Router, browserHistory } from 'react-router';

/* … */

class App extends Component {
  render() {
    return (
      <Router history={browserHistory}>
        <Route path="/" component={Something} />
      </Router>
    );
  }
}

No need to change the webpack config!

All 4 comments

Not quite, to add react-router you have to:

  1. Install it via npm install --save react-router
  2. Use react-router in your app!

For example, you could do something like this:

import { Router, Router, browserHistory } from 'react-router';

/* … */

class App extends Component {
  render() {
    return (
      <Router history={browserHistory}>
        <Route path="/" component={Something} />
      </Router>
    );
  }
}

No need to change the webpack config!

@mxstbr I recommend you update your answer to use the browserHistory:

<Router history={browserHistory}>
...

@mxstbr Why did you duplicate Router twice in import {Router, Router,...

@samayo One of them should have been Route

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rdamian3 picture rdamian3  Â·  3Comments

wereHamster picture wereHamster  Â·  3Comments

fson picture fson  Â·  3Comments

fson picture fson  Â·  3Comments

xgqfrms-GitHub picture xgqfrms-GitHub  Â·  3Comments