我正在使用 React Router 进行服务器端渲染,我想在某个 Route 上运行一个特殊操作,实际上是在 DefaultRoute 上。我应该如何为此创造条件?我尝试了以下代码:
var React = require("react");
var Router = require("react-router");
var DefaultRoute = Router.DefaultRoute;
var Link = Router.Link;
var Route = Router.Route;
var RouteHandler = Router.RouteHandler;
var routes = [
React.createElement(Route, {name: "root", path: "/", handler: Root},
React.createElement(DefaultRoute, {handler: Default})
)
]
var Default = React.createClass({
displayName: "Default",
render: function () {
return React.createElement("h2", null, "Default");
}
});
var Root = React.createClass({
displayName: "App",
render: function () {
return (
React.createElement(RouteHandler, null)
);
}
});
app.get('*', function (req, res) {
Router.run(routes, req.path, function (Root, state) {
if (React.createElement(Root, null).constructor.displayName === 'Default'){
//run specific action
}
});
});
但是我无法以这种方式获取组件的名称。有没有其他方法可以识别我在默认路由上?