Routing, Universal Applications (Nodejs, React), Error (0, _reactRouter.match) are not a function

I cannot fix this error ... I start the server, everything is fine, as I update localhost: 3000 Then it will show me an error:

TypeError: (0, _reactRouter.match) is not a function

I have a reactive router installed: "^ 4.0.0"

import Express from 'express'; import {RouterContext, match} from 'react-router'; import {renderToString } from 'react-dom/server'; import React from 'react'; import routes from './routes.js' var app = new Express(); app.set('view engine', 'ejs'); app.set('views',__dirname); ////////////////////////////////////////////////////////////////////// app.get('*', (req, res) => { match( { routes, location: req.url }, (err, redirectLocation, renderProps) => { if (err) { return res.status(500).send(err.message); } if (redirectLocation) { return res.redirect(302, redirectLocation.pathname + redirectLocation.search); } var markup; if (renderProps) { // if the current route matched we have renderProps markup = renderToString(<RouterContext {...renderProps}/>); } else { // otherwise we can render a 404 page markup = renderToString(<NotFoundPage/>); res.status(404); } // render the index template with the embedded React markup return res.render('index', { markup }); } ); }); ////////////////////////////////////////////////////////////////////////////////// var port = process.env.PORT || 3000; app.listen(port, ()=>{ console.log('Server is listening on port ' + port ); }); 
+5
source share
1 answer

Your code looks correct if you used a reaction router prior to version v4, but the reaction router v4 has violations in the entire code base, including the server rendering method. In version 4 there is a new component for creating a server - StaticRouter .

Take a look at the documentation for server rendering: https://reacttraining.com/react-router/web/guides/server-rendering

If you still want to use the match function as it is, you can use the jet router version below version 4. Take a look at my answer to a very similar question from yesterday , you can use the same template / example as another OP.

+3
source

Source: https://habr.com/ru/post/1265515/


All Articles