Woj*_*nka 6 javascript reactjs react-router isomorphic-javascript
我无法修复此错误...我启动服务器,一切正常,因为我刷新localhost:3000然后它显示一个错误:
TypeError:(0,_reactRouter.match)不是函数
我安装了"react-router":"^ 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 );
});
Run Code Online (Sandbox Code Playgroud)
如果您在v4之前使用了反应路由器,那么您的代码看起来是正确的,但是反应路由器v4在整个代码库中都有重大变化,包括服务器渲染方法.在v4中,有一个专门用于服务器渲染的新组件 - StaticRouter
.
请查看此处有关服务器呈现的文档:https: //reacttraining.com/react-router/web/guides/server-rendering
如果您仍然希望使用该match
功能,可以使用版本4以下版本的react-router.从昨天看一个非常相似的问题我的答案,您可能使用相同的样板/示例作为另一个OP.
归档时间: |
|
查看次数: |
4091 次 |
最近记录: |