简体   繁体   中英

How to do server side rendering in react despite all react router version conflicts?

I am trying to server-side rendering with react and react-router-dom I've done this before but with the new version, it is throwing me an error You should not use Switch outside a Router I think it is a version conflict but is there a solution ( react-router-config - You should not use outside a (it is inside a Router!) ).

server.js

import express from 'express';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import App from '../client/src/AppRoutes';
import { StaticRouter } from 'react-router-dom';
import { renderRoutes } from 'react-router-config'


const app = express();



app.get('*', (req, res) => {
    const routerContext = {};
    res.send(`
    <!doctype html>
    <html lang="en">
      <head>SSR</head>
      <body>
        <div id="root">${ReactDOMServer.renderToString(
            <StaticRouter location={req.url} context={routerContext}>
                    <div>{renderRoutes(App)}</div>
            </StaticRouter>
            )}</div>
<!--        <script src="/dist/main.js"></script>-->
      </body>
    </html>
  `);
});


app.listen(3000, (error) => {
    if (error) {
        return console.log('something bad happened', error);
    }
    console.log("listening on " + 3000 + "...");
});

AppRoutes.js

import PageA from './Container/PageA';
import PageB from './Container/PageB';
import Home from './Container/Home'
import MainPage from './Container/MainPage';



export default [
    {
        ...MainPage,
        routes:[
            {
              ...Home,
              path:'/',
              exact:true
            },
            {
                ...PageA,
                path:'/a',
                exact:true
            },
            {
                ...PageB,
                path:'/b',
                exact:true

            }
        ]
    }
]

package.json

{
  "name": "reactSSRwithCodeSplitting",
  "version": "1.0.0",
  "description": "",
  "main": "server/server.js",
  "scripts": {
    "test": "babel-node server/index.js",
    "start": "nodemon server/index.js",
    "build": "babel server --out-dir ./dist --source-maps",
    "serve": "node ./dist/index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "react": "^16.9.0",
    "react-dom": "^16.9.0",
    "react-loadable": "^5.5.0",
    "react-router-config": "^5.0.1",
    "react-router-dom": "^5.0.1"
  },
  "devDependencies": {
    "@babel/cli": "^7.5.5",
    "@babel/core": "^7.5.5",
    "@babel/node": "^7.5.5",
    "@babel/plugin-syntax-dynamic-import": "^7.2.0",
    "@babel/preset-env": "^7.5.5",
    "@babel/preset-react": "*",
    "@babel/register": "^7.5.5",
    "chai": "^4.2.0",
    "nodemon": "^1.19.1"
  }
}

Quick fix:

  1. Remove your node_modules
  2. Add "react-router": "^5.0.1" to your package.json
  3. Re-install with npm install .

Ensure that react-router , react-router-config , react-router-dom are in the same version.

What is wrong?

When I try to reproduce your error, I figure out that your package.json in the question is missing react-router dependency, which makes me unable to start the server.

And from your story, I guess that you have another version of react-router in your node_modules , which is out of sync with react-router-config and react-router-dom version 5.0.1.

So I try to reproduce the problem with this:

"react-router": "^4.3.0",
"react-router-config": "^5.0.1",
"react-router-dom": "^5.0.1"

Which will lead to the problem you mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM