2017-01-06 5 views
1

と一致していないルータ、サーバ側の反応:は、サーバー上のルータに反応することは、私が使用するためにここに与えられた例を、以下のいた場所

https://github.com/ReactTraining/react-router/blob/v2.8.1/docs/guides/ServerRendering.md

はどういうわけか、この単純なルータの設定と一致して指定された場所と一致していません。

import React from "react" 
import ReactDOMServer from 'react-dom/server' 
import { Route, match, RouterContext, IndexRoute } from 'react-router' 

const sampleRoutes = (
    <Route path="/"> 
     <IndexRoute component={() => <span>index</span>}/> 
     <Route path="z1" component={() => <span>z1</span>} /> 
     <Route path="z2" component={() => <span>z2</span>} /> 
    </Route> 
) 

const render = location => { 
    match({sampleRoutes, location}, (error, redirectLocation, renderProps) => { 
     if (error) { 
      console.error("error: "+ error) 
     } else if (redirectLocation) { 
      console.error("redirect to " + redirectLocation) 
     } else if (renderProps) { 
      var page = ReactDOMServer.renderToStaticMarkup(<RouterContext {...renderProps} />) 
      console.log(page) 
     } else { 
      console.error("location nof found: '"+ location +"'") 
     } 
    }) 
} 

render("/z1") 

「ルートが見つかりません/ z1」がコンソールに表示されています。

答えて

2

matchは、(それがthis commentを参照してください、あなたのためのオブジェクトにあなたの<Route>秒に変換します)routesオブジェクトを期待していますが、<Router>要素とそれを提供します。試してみてください:

const sampleRoutes = (
    <Route path="/"> 
    <IndexRoute component={() => <span>index</span>}/> 
    <Route path="z1" component={() => <span>z1</span>} /> 
    <Route path="z2" component={() => <span>z2</span>} /> 
    </Route> 
) 

第二に、あなたはmatchに次のオブジェクトを渡しています

{sampleRoutes, location} 

に換算さ:あなたが本当にしたい場合は

{ sampleRoutes: sampleRoutes, location: location } 

{ routes: sampleRoutes, location } 
+0

感謝、私はあなたの提案された変化を適用し、私はそれでも "ルートが見つかりません"と表示されます。私は反応ルータv2.8.1(誤ったリンクを貼り付けた)を使用していました。 –

+0

あなたの他の問題を修正するために更新されました。問題はまだありますが、あなたの変更を適用するよう質問を更新しました。 –

+0

ありがとう、問題は解決しました! –