服务器端反应路由器不会渲染我的路由

服务器端反应路由器不会渲染我的路由

问题描述:

我与版本1.0.0-rc1和我的匹配函数将不会正确呈现我的路线。服务器端反应路由器不会渲染我的路由

这是我的服务器

import express from 'express'; 
import React from 'react'; 
import createLocation from 'history/lib/createLocation' 
import Router, { match, RoutingContext } from 'react-router'; 
import createRoutes from './create-routes'; 

const app = express(); 
const routes = createRoutes(); 

app.use((req, res) => { 
    let location = createLocation(req.url); 

    match({ routes, location }, (error, redirectLocation, renderProps) => { 
    if (redirectLocation) 
     res.status(301).redirect(redirectLocation.pathname + redirectLocation.search) 
    else if (error) 
     res.status(500).send(error.message) 
    else if (renderProps == null) 
     res.status(404).send('Not found') 
    else 
     res.send(React.renderToString(<RoutingContext {...renderProps}/>)) 
    }); 
}); 

export default app; 

这是我的路线

import React from 'react'; 
import { Route } from 'react-router'; 
import Application from './components/Application.react'; 
import Home from './components/Home.react'; 

export default function() { 
    return (
    <Route path="/" component={Application}> 
     <Route path="home" component={Home} /> 
    </Route> 
); 
} 

我该怎么办错了呢?当我要求回家时,它应该呈现<h1>Home</h1>而不是<h1>Application</h1>。就这么简单。

我此基础上本https://github.com/rackt/react-router/blob/master/docs/guides/advanced/ServerRendering.md

谢谢

您正在使用子路径。这意味着,当您首次访问“/ home”时,应用程序已呈现,然后通过应用程序组件内部的this.props.children注入并可使用子主页。 为一个简单的例子见here

试试这个(不嵌套):

<Route path="/" component={Application}> 
<Route path="/home" component={Home} /> 

如果要呈现在家庭成分使用

render() { 
    return (
     <div> 
      <h1>Application</h1> 
      { this.props.children } 
     </div> 
    ); 
} 
+0

您的应用程序组件OMG ......这样的一个noob错误...谢谢! –

+0

编辑应用程序组件的呈现在:) – Herku

+0

请参阅问题,您将如何呈现childRoutes,然后在服务器端? –