简体   繁体   English

违反JS不变规则

[英]React JS Invariant Violation

I am fairly new to ReactJS so I am still learning the rope, but I am getting an error when trying to incorporate react-router into my application. 我对ReactJS相当陌生,因此我仍然在学习绳子,但是在尝试将react-router集成到我的应用程序时遇到错误。 The lovely error that I am getting is: 我收到的可爱错误是:

Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. 未捕获的不变违规:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。 Check the render method of App . 检查App的渲染方法。

I have searched around for this error and saw a couple of fixes that I don't believe apply to my situation. 我到处搜索了此错误,并发现了一些我认为不适用于我的情况的修复程序。 The most common error is missing a 'return' method, but I am pretty sure I am not missing one. 最常见的错误是缺少“返回”方法,但我敢肯定我不会错过任何一种。

Here is the code in my index.js: 这是我的index.js中的代码:

import './styles/main.css'

import Contact from './components/Contact.jsx';
import App from './components/App.jsx';

import React from 'react';
import ReactDOM from 'react-dom';


import { Router, Route, hashHistory } from 'react-router'

ReactDOM.render((
  <Router history={hashHistory}>
    <Route path="/" component={App}>
      <Route path="/contact" component={Contact}/>
    </Route>
  </Router>
), document.getElementById('app'))

And my App.jsx looks like: 我的App.jsx看起来像:

import React from 'react';

import Link from 'react-router'

export default class App extends React.Component {
    render() {
        return(
          <div className="wrapper">
            <header>
              <h1>Rest Test Contact List</h1>
              <nav>
                <ul className="navigation">
                  <li><Link to="/contact">Contact</Link></li>
                  <li><Link to="/about">About</Link></li>
                </ul>
              </nav>
            </header>
            {this.props.children}
          </div>
      )
    }
}

And help on this error would be much appreciated. 对于此错误的帮助将不胜感激。 I have been scratching my head on this for a few days. 我已经为此努力了几天。

Thanks! 谢谢!

In App.jsx you're not destructuring your import of Link correctly. App.jsx您没有正确地破坏Link的导入。 It should be 它应该是

import { Link } from 'react-router';

That would cause the use of Link as a component (in your App's render method) to be undefined. 这将导致未定义将Link用作组件(在您应用的render方法中)的使用。

The Destructuring Assignment syntax makes it possible to extract data from objects and save them as such at the same time. 解构分配语法使从对象提取数据并同时保存它们成为可能。 ES6 introduced this shorthand for ease of use. ES6引入了此简写以方便使用。 In ES5 this would look like: 在ES5中,它看起来像:

var Link = require('react-router').Link

But remember, this only works for when you're accessing children of an object (the submodule in the case of the imported modules you're using). 但是请记住,这仅在访问对象的子项时才有效(在使用导入模块的情况下为子模块)。 That is why you do not need it when importing React or ReactDOM; 这就是为什么在导入React或ReactDOM时不需要它的原因; you're importing the entire thing. 您正在导入整个内容。 But that doesn't mean likewise can't be done for those other libraries. 但这并不意味着其他库也同样无法完成。 For React, you could import Component directly, and reference it by name 对于React,您可以直接导入Component并按名称引用

import React, { Component } from 'react';

And then you'd use it like: 然后您可以像这样使用它:

export default class App extends Component { ... }
                                 ^

Notice that you're not using React.Component ? 注意您没有使用React.Component吗? Its because we've already imported it using the same destructuring syntax as with Link from react-router . 这是因为我们已经使用与react-router Link相同的解构语法导入了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM