javascript 反应路由器:type.toUpperCase 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30868539/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:50:56  来源:igfitidea点击:

React-router: type.toUpperCase is not a function

javascriptreactjsreact-router

提问by Fakefish

when I first to use react-router, but page gives me this error below

当我第一次使用 react-router 时,但页面在下面给了我这个错误

React-router: type.toUpperCase is not a function

反应路由器:type.toUpperCase 不是函数

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

var Route = Router.Route;

var App = React.createClass({

  render: function() {
    return (
      <div>App</div>
    );
  }
});

React.render((
  <Router>
    <Route path="/" component={App} />
  </Router>
), document.getElementById('app'));

it seems nothing wrong from the document, some one could help me?

从文件上看似乎没什么问题,有人可以帮助我吗?

where error comes here

哪里出错了

function autoGenerateWrapperClass(type) {
    return ReactClass.createClass({
      tagName: type.toUpperCase(),
      render: function() {
        return new ReactElement(
          type,
          null,
          null,
          null,
          null,
          this.props
        );
      }
    });
  }

回答by kumar303

The error you posted is cryptic but what it means is you are trying to render a component out of a variable that is not a real component. For example, you could do this and get the same kind of error:

您发布的错误很神秘,但这意味着您正在尝试从不是真实组件的变量中渲染组件。例如,您可以这样做并得到相同类型的错误:

render: function() {
  var Thing = {};  // not a component
  return <Thing />;  // same error
}

In the code you posted, <Router>maps to a variable that is a module, not a component. You can fix it with a new variable declaration:

在您发布的代码中,<Router>映射到作为模块而非组件的变量。您可以使用新的变量声明来修复它:

var React = require('react');

var routerModule = require('react-router');
var Router = routerModule.Router;  // component

var Route = routerModule.Route;

var App = React.createClass({

  render: function() {
    return (
      <div>App</div>
    );
  }
});

React.render((
  <Router>
    <Route path="/" component={App} />
  </Router>
), document.getElementById('app'));

回答by Swapnil Bhikule

I was getting the same error. Then I figure out that I did mistake while exporting my components. In one component I wrote module.export instead of module.exports. So, please check if you have done the same mistake.

我遇到了同样的错误。然后我发现我在导出组件时犯了错误。在一个组件中,我编写了 module.export 而不是 module.exports。所以,请检查您是否犯了同样的错误。

回答by marcel

change the require statements:

更改 require 语句:

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

回答by ololo

if you use react-router v0.13.3 replace Routeron Route, like this:

如果您使用 react-router v0.13.3 替换 Route上的 Route r,如下所示:

var routes = (
  <Route>
    <Route path="/" component={App} />
  </Route>
);

Router.run(routes, function(Root) {
 React.render(<Root />, document.getElementById('app'));
});

回答by Pico12

I had the same error, it was circle dependency.

我有同样的错误,它是循环依赖。

component1.jsx :

组件 1.jsx :

var Component1 = require('./component2.jsx');

component2.jsx :

组件2.jsx:

var Component2 = require('./component1.jsx');

Seen like this, this is obvious, but the error message is very opaque.

这样看,这很明显,但是错误信息很不透明。