React / Express - 在match / RouterContext中需要道具'undefined'

时间:2017-07-07 16:43:00

标签: reactjs express react-router

我正在为我的React / Express应用程序实现服务器端呈现 - 该应用程序实际上正在按预期工作,但我在服务器上收到与match()和{{1}相关的警告我想理解并解决。

基本上,在RouterContext的回调函数中,参数match都评估为(err, redirect, props)

undefined(快速应用根):

index.js

因此,如果我为'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)) }) }) console.log()err投放redirect,则会全部返回props。这会导致我的服务器返回警告

undefined

再次,令我惊讶的是,我的应用似乎工作正常 - 我可以刷新页面,直接访问网址(即'localhost:3000 / person / 1234'),但这些警告仍然存在。也许我不理解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 (可能)的需求。

无论如何,我想解决这个问题,因为我想使用这些参数来设置一些条件......对此的任何指导都将非常感激。

更新 我已经意识到RouterContexterr应该默认为redirect(除非出现错误或重定向),但无论我做什么,undefined似乎都是也坚持未定义。

来自https://knowbody.github.io/react-router-docs/api/match.html:“如果所有三个参数均为props,则表示找不到与指定位置匹配的路线。”

但我不明白 - 该应用似乎工作正常。是什么给了什么?

2 个答案:

答案 0 :(得分:1)

感谢@taha指出我正确的方向。事实证明问题是我使用createRoutes()以及我没有正确访问路线这一事实。

match的调用必须是

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

答案 1 :(得分:0)

matrix(t(m), ncol = 1) [,1] [1,] 1 [2,] 2 [3,] 3 [4,] 4 [5,] 5 [6,] 6 [7,] 7 [8,] 8 [9,] 9 已从 react-router (少数发行版)中删除。

createRoutes

您应手动创建/准备路线并将其导出为独立模块(下面的代码段取自official docs

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