javascript react-router Uncaught TypeError:无法读取未定义的属性“toUpperCase”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30990726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
react-router Uncaught TypeError: Cannot read property 'toUpperCase' of undefined
提问by pedronalbert
I am trying to use the react-router but when I write a simple route doesn't work and the console show Uncaught TypeError: Cannot read property 'toUpperCase' of undefined.
我正在尝试使用 react-router 但是当我写一个简单的路由不起作用并且控制台显示 Uncaught TypeError: Cannot read property 'toUpperCase' of undefined。
Otherwise when I use without the react-router that work well
否则,当我在没有运行良好的反应路由器的情况下使用时
var React = require('react');
var ReactRouter = require('react-router');
var Router = ReactRouter.Router;
var Route = ReactRouter.Route;
var App = React.createClass({
render: function () {
return (
<div>Hello World</div>
);
}
});
React.render((
<Router>
<Route path="/" component={App} />
</Router>
), document.body);
The error is from this line of the react library
错误来自反应库的这一行
function autoGenerateWrapperClass(type) {
return ReactClass.createClass({
tagName: type.toUpperCase(), //<----
render: function() {
return new ReactElement(
type,
null,
null,
null,
null,
this.props
);
}
});
}
采纳答案by Dan Abramov
You are using examples from 1.0 Beta docs, but you are likely running the latest stable version (0.13). The docs in master are from 1.0 Beta, that's the source of confusion.
您正在使用 1.0 Beta 文档中的示例,但您可能正在运行最新的稳定版本 (0.13)。master 中的文档来自 1.0 Beta,这是混淆的根源。
Read the docs for the latest stable version: https://github.com/ReactTraining/react-router/tree/master/docs
阅读最新稳定版本的文档:https: //github.com/ReactTraining/react-router/tree/master/docs
回答by diomampo
I'm still wrapping my head around react-router, but the first thing I'd check is the version you're using and the docs your referencing. I was able to get it working by doing the following...
我仍然在思考 react-router,但我首先要检查的是您使用的版本和您引用的文档。我能够通过执行以下操作来让它工作......
var React = require('react');
var ReactRouter = require('react-router');
var Route = ReactRouter.Route;
var App = React.createClass({
render: function () {
return (
<div>Hello World</div>
);
}
});
var routes = (
<Route handler={App}>
</Route>
);
window.onload = function() {
ReactRouter.run(routes, ReactRouter.HistoryLocation, function(Root, state) {
React.render(
<Root />,
document.getElementById('main')
)
});
};
Basically what PhInsidesaid, but wrapped in window.onload
基本上是PhInside所说的,但包含在 window.onload 中
回答by Phi Nguyen
My guess is when React.render is execute, the Router is not there yet. try this
我的猜测是当 React.render 执行时,Router 还不存在。试试这个
var routes = (
<Route handler={App}>
</Route>
);
Router.run(routes,(Root) => {
React.render(<Root/>, document.body);
});
回答by whitetrefoil
Try <Route>
instead of <Router>
in React.render()
.
尝试<Route>
而不是<Router>
in React.render()
。