在`RouteHandler`中没有指定必需的上下文`routeDepth`

时间:2015-07-25 14:42:07

标签: reactjs react-router isomorphic-javascript

我一直试图将React应用程序原型化几个小时,并且在尝试实施路线时遇到困难。我遇到的错误是:

Warning: Failed Context Types: Required context `routeDepth` was not specified in `RouteHandler`.
Warning: Failed Context Types: Required context `router` was not specified in `RouteHandler`.
Warning: owner-based and parent-based contexts differ (values: `undefined` vs `1`) for key (routeDepth) while mounting RouteHandler
Uncaught TypeError: Cannot read property 'getRouteAtDepth' of undefined

它是一个同构应用程序(在服务器和客户端上呈现),显然我读到的所有内容都是关于 React或React-Router被加载两次。但我还没有看到解决这个问题的正确方法以及导致它的原因(是Gulp配置还是React导入文件的方式?)。

这就是我所拥有的:

server.js

var express = require('express');
var http = require('http');
var app = express();
var React = require('react');
var Router = require('react-router');

require("babel/register");

var routes = require('./app/routes.js');

app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));

app.use(function(req, res) {
  Router.run(routes, req.path, function(Handler) {
    var html = React.renderToString(React.createElement(Handler));
    var page = res.render(__dirname + '/server/views/index.ejs', {reactOutput: html});
  });
});

var server = app.listen(8080, function(req, res) {
    console.log('Server started');
});

require('./server/routes/routes.js')(app, io);

index.ejs

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <title>React App</title>
    <link rel="stylesheet" href="/css/style.css"/>
    <link href='http://fonts.googleapis.com/css?family=Lato:100,300,400' rel='stylesheet' type='text/css'>
</head>
<body>
    <div id="app"><%- reactOutput %></div>
    <script src="/bower_components/socket.io-client/socket.io.js"></script>
    <script src="/js/vendor.js"></script>
    <script src="/js/vendor.bundle.js"></script>
    <script src="/js/bundle.js"></script>
</body>
</html>

app.js (在gulp运行后将成为 bundle.js

var React = require('react');
var ReactApp = require('./components/App/ReactApp.js');
var Activity = require('./components/Activity/Activity.js');
var Router = require('react-router');
var routes = require('./routes.js');
var Route = Router.Route;
var Link = Router.Link;

Router.run(routes, Router.HashLocation, function(Handler) {
  React.render(<Handler/>, document.getElementById("app"));
});

routes.js

var React = require('react');
var Router = require('react-router');
var Route = Router.Route;
var Link = Router.Link;

var App = require('./components/App/ReactApp.js');
var ActivitiesList = require('./components/ActivitiesList/ActivitiesList.js');
var Activity = require('./components/Activity/Activity.js');
var Task = require('./components/Task/Task.js');


export default (
  <Route path="/" handler={App}>
    <Route path="/activity" component={Activity}/>
  </Route>
);

谢谢!

1 个答案:

答案 0 :(得分:1)

我回答了我自己的问题,因为我做了一些我做错的事情并认为值得分享:

  1. 由于未知原因,当我将ReactApp组件的语法从ES5更改为ES6时,引发此问题的错误消失了。 (我没有在问题中发布此文件);我不知道这是否是浏览器要求,但我最终将我的所有ES5语法都改为ES6,如果你从React开始,我建议你做同样的事情,因为大多数例子都是ES6。
  2. server.js中,我对服务器路由文件(require('./server/routes/routes.js'))的要求应该在Router.run之前,否则我的所有api请求都会被React-Router拦截;
  3. <Route path="/activity" component={Activity} />中,我将component替换为handler
  4. 如果您不想在网址中使用哈希routes.js,请Router.HashLocationRouter.HistoryLocation之前替换#