【发布时间】:2015-11-19 10:32:05
【问题描述】:
我正在关注 react-router 指南中报告的示例
var App = React.createClass({
render: function () {
return <div>Hi</div>;
}
});
var routes = (
<Route handler={App} path="/" />
);
// if using express it might look like this
app.use(function (req, res) {
// pass in `req.url` and the router will immediately match
Router.run(routes, req.url, function (Handler) {
var content = React.renderToString(<Handler/>);
res.render('main', {content: content});
});
});
很简单,不是吗?相反,这是我的代码:
export function index(req: express.Request, res: express.Response, next: Function) {
Router.run(routes, req.url, (Handler, state) => {
fs.readFileAsync(
path.join(__dirname, '..', 'mockData/airport-codes.csv'),
'utf-8'
).then((content) => {
return csv.parseAsync(content);
}).then((parsedContent: Array<string[]>[]) => {
ResponseHelper.renderTemplate('index', res, {
output: React.renderToString(React.createElement(Handler, {
header: [
"ID", "Type", "Name", "Latitude (deg)", "Longitude (deg)", "Elevation", "Continent", "Country ISO", "Region ISO", "Municipality", "GPS Code", "IATA Code", "Local Code"
],
initialData: parsedContent
}))
});
}).catch(next);
});
}
基本上我所做的是获取数据并传递给 Handler 以初始化组件。这是 react 中的文件路径:
import { Route } from "react-router";
import * as React from "react";
import Excel from "../components/Excel";
export default (
<Route handler={Excel} path="/" name="excel" />
);
这是前端的入口点
import * as React from "react";
import * as Router from "react-router";
import routes from "./shared/routes";
Router.run(routes, Router.HistoryLocation, (Root, state) => {
React.render(<Root />, document.getElementById('app'));
});
我的问题是,当我启动应用程序时,我收到以下警告:
Warning: Failed propType: Required prop `header` was not specified in `Excel`. Check the render method of `Router`.
Warning: Failed propType: Required prop `initialData` was not specified in `Excel`. Check the render method of `Router`.
因此出现错误。你知道在这种情况下哪种方式传递 props 是正确的吗?
编辑
错误信息
Uncaught SyntaxError: Unexpected token &
1../components/Excel @ bundle.js:4s @ bundle.js:1e @ bundle.js:1(anonymous function) @ bundle.js:1
【问题讨论】:
-
我修复了服务器中的错误,现在它可以从服务器正确呈现,但不能在客户端呈现。似乎服务器中的渲染工作正常,但在客户端却没有
标签: javascript node.js typescript reactjs react-router