我正在尝试使路由器v1.0运行,但是当我为服务器端渲染编写路由器时,我在浏览器中收到此错误:React is undefined
router.js
:
'use strict';
import { Router, Route, match, RoutingContext } from 'react-router';
import routes from './routes';
import {renderToString} from 'react-dom/server';
export default function(req, res, next) {
match({routes, location: req.url}, (error, redirectLocation, renderProps) => {
if (error) {
res.status(500).send(error.message)
} else if (redirectLocation) {
res.redirect(302, redirectLocation.pathname + redirectLocation.search)
} else if (renderProps) {
let markup = renderToString(<RoutingContext {...renderProps} />);
res.render('index', {markup: markup});
} else {
res.status(404).send('Not found')
}
})
};
我在快递server.js
import express from 'express';
import path from 'path';
import favicon from 'serve-favicon';
import logger from 'morgan';
import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';
import exphbs from 'express-handlebars';
import router from '../shared/routes/router';
import routes from "../shared/routes/routes";
const app = express();
// view engine setup
app.engine('handlebars', exphbs({defaultLayout: 'main', extname: '.handlebars'}));
app.set('views', './views'); // ????
app.set('view engine', 'handlebars');
// uncomment after placing your favicon in /public
app.use(favicon('favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
//app.use(require('less-middleware')(path.join(__dirname, 'dist')));
app.use(express.static('./public'));
app.use(router);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
//.... the rest of code
我和here做的很相似。 当我查看已编译的代码(使用babelify)时,我看到:
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function (req, res, next) {
(0, _reactRouter.match)({ routes: _routes2.default, location: req.url }, function (error, redirectLocation, renderProps) {
if (error) {
res.status(500).send(error.message);
} else if (redirectLocation) {
res.redirect(302, redirectLocation.pathname + redirectLocation.search);
} else if (renderProps) {
res.status(200).send((0, _server.renderToString)(React.createElement(_reactRouter.RoutingContext, renderProps)));
} else {
res.status(404).send('Not found');
}
});
};
var _reactRouter = require('react-router');
var _routes = require('./routes');
var _routes2 = _interopRequireDefault(_routes);
var _server = require('react-dom/server');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
;
但是没有浏览器,babelify和路由器反应通过将所需的模块带入代码来处理这个问题?
答案 0 :(得分:2)
您可以从已转换的输出中看到jsx已转换为:
React.createElement(_reactRouter.RoutingContext, renderProps)
这需要引用React
才能运行。
只需在router.js
的顶部添加导入即可解决问题:
'use strict';
import { Router, Route, match, RoutingContext } from 'react-router';
import routes from './routes';
import {renderToString} from 'react-dom/server';
import React from 'react';
Browserify只了解如何读取您导入的模块。因此,在这种情况下,您必须明确导入React
。