mern-starter icon indicating copy to clipboard operation
mern-starter copied to clipboard

Upgrade to React Router 4

Open nreoch25 opened this issue 7 years ago • 9 comments

Are there any plans to upgrade react-router to version 4? It would be interesting to hear if/how you guys plan on tackling that.

nreoch25 avatar Mar 31 '17 02:03 nreoch25

I really need this. react-router v4 really brought a lot of changes.

ebukahills avatar May 24 '17 22:05 ebukahills

Good luck for new comer

infacq avatar Jul 25 '17 16:07 infacq

+1

If this projects gets too dated it could discourage people from using this stack.

Nicholson85 avatar Jul 26 '17 11:07 Nicholson85

Anyone did it on his own? Can you give some advice how to migrate mern project to react-router v4?

Spisaczek avatar Aug 01 '17 16:08 Spisaczek

Well, to preface I don't have much experience with using React Router. But looking at the v4 docs, I managed to make a basic working version:

/server/server.js

import routes from '../client/routes'; 

...

// Server Side Rendering based on routes matched by React-router.
const reduxProvider = React.createFactory(Provider);
app.use((req, res, next) => {
  const context = {};
  const store = configureStore();
  const initialView = renderToString(
    reduxProvider(
      { store },
      <StaticRouter
        location={req.url}
        context={context}
      >
        <Provider store={store}>
          <IntlWrapper>
            {routes}
          </IntlWrapper>
        </Provider>
      </StaticRouter>
    )
  );
  const finalState = store.getState();

  if (context.url) {
    res.redirect(302, context.url);
  } else {
    res.set('Content-Type', 'text/html')
       .status(200)
       .end(renderFullPage(initialView, finalState));
  }
  return next();
});

/client/App.js

import { createBrowserHistory } from 'history';

const history = createBrowserHistory();

// Import Routes
import routes from './routes';

export default function App(props) {
  return (
    <Provider store={props.store}>
      <IntlWrapper>
        <Router history={history}>
          {routes}
        </Router>
      </IntlWrapper>
    </Provider>
  );
}

/client/routes.js

export default (
  <Route path="/" component={App} />
);

/client/modules/App/App.js

import { Switch, Route } from 'react-router';

...

<div className={styles.container}>
  <Switch>
    <Route
      exact path="/"
      component={require("your-page-link-here").default}
    />
    <Route
      path="/home"
      component={require("your-page-link-here").default}
      }}
    />
    <Route path="*" component={require('default-switch-case').default} />
  </Switch>
</div>

Caveats:

  • React router v4 got rid of getComponent; I replaced it with component, which works but doesn't have code splitting. I've been experimenting with react-loadable but am running into issues.
  • Routes are located in /client/App/App.js instead of /client/routes.js, as routes cannot have children anymore in v4.

therladbsgh avatar Aug 04 '17 02:08 therladbsgh

I have been working on a new mern stack based on this one and react-universally. After i ran into same issue as you guys.

With react 15 and react-router 4 its working well. But with react 16 i ran into some issues with tests because react-addons-test-utils has been decapitated. If you wanna try my repo you can click at the link below.

Link to the repo https://github.com/Ballpin/mern-starter2

Ballpin avatar Oct 15 '17 17:10 Ballpin

I decided to create a very simple updated version of this stack. Tech used is React 16, React-router-4, Webpack3, Redux, Redux-saga, code Splitting with Loadable-components, HMR, SSR.

I've kept everything very simple so people new to any of these technologies shouldn't have a problem understanding what's going on.

Please let me know in the issues at the below repo if you're having any problems running this and please share.

https://github.com/nreoch25/mern-boilerplate

nreoch25 avatar Jan 03 '18 18:01 nreoch25

This has been officially moved to V2.8.0 and will be taken care of soon (working on V2.5)

mannyhenri avatar Jun 02 '18 00:06 mannyhenri

Hi @nreoch25 ,

Since this project is deprecated, I started a new MERN project in Typescript. https://github.com/shanhuiyang/TypeScript-MERN-Starter I think the new MERN project can fulfil your requirement. It is featured by:

  • RESTful-style, powered by an embedded oauth2 server and passport.js, this project separate client and server clearly.
  • React-router 4.0, with it you can easily define client routes and manage them.
  • Redux, with it you can easily manage client states.
  • Almost ready for a community app. We modelled User as well as Article. This is a real starter for who would like to build an community app on MERN.
  • The client code is created from create-react-app, so now you can get rid of annoying configurations for babel and webpack.

The project is still under construction but it already works well now. Please have a try, issues and feedbacks are welcome.

shanhuiyang avatar Jun 16 '19 14:06 shanhuiyang