2017-07-07 8 views
0

私はReact/Expressアプリケーションのサーバー側レンダリングを実装しようとしています - 実際にアプリケーションは必要に応じて動作していますが、サーバーはmatch()に、RouterContextには理解して解決したいと思っています。React/Express - match/RouterContextで必要な小道具 'undefined'

基本的にmatchのコールバック関数では、引数(err, redirect, props)はすべてundefinedと評価されています。

index.js(エクスプレスアプリのルート):私はerrredirect、およびpropsのためにconsole.log()を投げる場合

'use strict' 
require('babel-register')({ 
    presets: ['es2015', 'react'] 
}); 

const express = require('express') 
const path = require('path') 
const app = express() 
const React = require('react') 
const reactDomServer = require('react-dom/server') 
const routes = require('./src/routes.jsx') 
const reactRouter = require('react-router') 
let { match, RouterContext, createRoutes } = reactRouter 

app.get('*', (req, res) => { 
    match({ routes: createRoutes(routes), location: req.url }, (err, redirect, props) => { 
     const appHtml = renderToString(React.createElement(RouterContext, props)) 
     res.send(renderPage(appHtml)) 
    }) 
}) 

だから、彼らはすべてのundefinedとして戻ってきます。 、:私は、ページをリフレッシュすることができ、アクセスURLを直接(すなわち、「3000 /人/ 1234はlocalhost」) - これが私のサーバーが警告

再び
Warning: Failed prop type: The prop `router` is marked as required in `RouterContext`, but its value is `undefined`. 
    in RouterContext 
Warning: Failed prop type: The prop `location` is marked as required in `RouterContext`, but its value is `undefined`. 
    in RouterContext 
Warning: Failed prop type: The prop `routes` is marked as required in `RouterContext`, but its value is `undefined`. 
    in RouterContext 
Warning: Failed prop type: The prop `params` is marked as required in `RouterContext`, but its value is `undefined`. 
    in RouterContext 
Warning: Failed prop type: The prop `components` is marked as required in `RouterContext`, but its value is `undefined`. 
    in RouterContext 
Warning: Failed child context type: The child context `router` is marked as required in `RouterContext`, but its value is `undefined`. 
    in RouterContext 

を返すようになり、私の驚きに、私のアプリがうまく動作するようですこれらの警告はそのままです。たぶん私はRouterContext(おそらく)のニーズを理解していない。

私はいくつかの条件を設定するためにそれらの引数を使用したいので、これを解決したいと思います...これについてのガイダンスは非常に高く評価されます。

更新 私はerrredirectundefinedをデフォルト(エラーがありますしない限り、またはリダイレクト)することになっていることに気づいてきましたが、関係なく、私は何をすべきか、propsは思わないが、同様未定義に貼り付けることができます。

https://knowbody.github.io/react-router-docs/api/match.html: "3つのパラメータがすべてundefinedである場合、これは指定された場所に一致するルートが見つかりませんでした。"

しかし、私は理解していない - アプリはうまくいくようだ。何がありますか?

+0

が 'err'はエラーがある場合を除きundefined''をデフォルトになっていることが必要です。 'props'も定義されていなければ、いくつかのエラーがあるのですか? – taha

+0

はい - 私の質問の最後に「更新」があります。あなたはcreateRoutesを使用しないことについて正しくありましたが、ここでは適切ではありません。私はそれを理解しました、私の答えを見てください。 – skwidbreth

答えて

1

感謝これに。問題は私がcreateRoutes()を使用したことと、私がルートに正しくアクセスしていなかったことの両方であることが分かります。 match

コールは

match({ routes: routes.default, location: req.url }, (err, redirect, props) => { /* etc. */ } 
+0

'routes:routes.default' :)それほど難しくありませんでした!私はあなたの問題を解決してうれしいです:D幸せなコード – taha

+1

私は簡単に何かに対して私の頭をバッシング一日を過ごした。 – skwidbreth

0

createRoutesreact-router(数ヶ月前)から削除されました。

let { match, RouterContext, createRoutes } = reactRouter 
// 'createRoutes' is undefined 

あなたはあなたのルートを手動で準備し、スタンドアロンモジュールとしてエクスポート/作成する必要があります(以下のスニペットはofficial docsから取られている)正しい方向に私を指しているため@tahaへ

module.exports = (
    <Route path="/" component={App}> 
    <IndexRoute component={Home}/> 
    <Route path="/repos" component={Repos}> 
     <Route path="/repos/:userName/:repoName" component={Repo}/> 
    </Route> 
    <Route path="/about" component={About}/> 
    </Route> 
) 
+0

私は 'react-router' v 3.0.2を使用しています。' createRoutes'はそのバージョンで有効な関数であり、 'const routes = require( './ src/routes。jsx ') 'は、あなたが示したようにスタンドアローンのモジュールを指します。うーん... – skwidbreth

関連する問題