我一直试图将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>
);
谢谢!
答案 0 :(得分:1)
我回答了我自己的问题,因为我做了一些我做错的事情并认为值得分享:
server.js
中,我对服务器路由文件(require('./server/routes/routes.js')
)的要求应该在Router.run
之前,否则我的所有api请求都会被React-Router拦截; <Route path="/activity" component={Activity} />
中,我将component
替换为handler
。routes.js
,请Router.HashLocation
在Router.HistoryLocation
之前替换#
。