使用react-router的“匹配”

时间:2017-03-09 19:40:49

标签: javascript reactjs redux react-router serverside-rendering

我有一个redux应用程序设置,通过express提供服务器端渲染。我有兴趣“过滤”服务器渲染的路由,基本上忽略了某些路由。这样做的主要原因是忽略需要完成令牌授权的API请求的路由。

server.js

import Express from 'express';
import getRoutes from './routes';
import { match, createMemoryHistory } from 'react-router';

app.use((req, res) => {
  const allRoutes = getRoutes(store);
  match({ routes: allRoutes, location: req.url }, (error, redirectLocation, renderProps) => {
    ...
  });
});

routes.js

import { IndexRoute, Route } from 'react-router';
import {
  App,
  HomePage,
  About,
  Contact,
  RestrictedPage
} from 'containers'

export default () => (
  <Route path="/" component={App}>
    <IndexRoute component={HomePage} />
    <Route path='/about' component={About} />
    <Route path='/contact' component={Contact} />
    <Route path='/restricted/:id' component={RestrictedPage} />
  </Route>
);

我基本上只是不想服务器渲染RestrictedPage。我希望也许可以使用类似的东西:

<Route path='/restricted/:id' component={RestrictedPage} noServerRender={true} />

然后在server.js中过滤,例如:

match({ routes: filterRoutes(routes), location: req.url }, ...

match所采用的参数似乎只是getRoutes()返回的完整对象。

无论如何,任何想法都将不胜感激。提前致谢。

0 个答案:

没有答案