简体   繁体   English

反应可重用组件错误(es6)

[英]react reusable components error (es6)

constantly getting these two errors in console: 在控制台中经常出现这两个错误:

Warning: React.createElement: type should not be null, undefined, boolean, or >number. 警告:React.createElement:type不应为null,undefined,boolean或> number。 It should be a string (for DOM elements) or a ReactClass (for composite >components). 它应该是一个字符串(对于DOM元素)或一个ReactClass(对于复合>组件)。 Check the render method of Embedded . 检查Embedded的render方法。

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

what i'm trying to do is have my Embedded class contain many Prop classes within it. 我想要做的是让我的Embedded类包含许多Prop类。

 import React from 'react'; import ReactDOM from 'react-dom'; class Prop extends React.Component { constructor () { super(); } render() { console.log(this); return <span> I am a { this.props.position } </span> } } ReactDOM.render(<Prop position="developer"/>, document.getElementById('prop-passed')); 

 import React from 'react'; import ReactDOM from 'react-dom'; import Prop from './prop.jsx'; class Embedded extends React.Component { constructor() { super(); } render() { let lines = this.props.sentences.map((item) => { return <li key={ Math.random(1, 5) }><Prop position={ item.position }/></li> }); return ( <div> { lines } </div> ); } } ReactDOM.render(<Embedded sentences={[ { id: 1, position: 'dog' }, { id: 2, position: 'cat' }, { id: 3, position: 'bear' }]}/>, document.getElementById('embedded')); 

EDIT: 编辑:

i've essentially removed the lines at the bottom of each snippet i provided earlier and created another file to put there instead but I am still getting the same errors. 我基本上删除了我之前提供的每个片段底部的行,并创建了另一个文件放在那里,但我仍然得到相同的错误。

i don't want to put all my classes in the same file because i would like them separate and easier to find when I need to look for them again. 我不想把我的所有课程都放在同一个文件中,因为我希望它们分开,当我需要再次查找它们时更容易找到它们。

 import React from 'react'; import ReactDOM from 'react-dom'; import Simple from './simple.jsx'; import Prop from './prop.jsx'; import Embedded from './embedded.jsx'; ReactDOM.render(<Simple/>, document.getElementById('simple')); ReactDOM.render(<Prop position="developer"/>, document.getElementById('prop-passed')); ReactDOM.render(<Embedded sentences={[ { id: 1, position: 'dog' }, { id: 2, position: 'cat' }, { id: 3, position: 'bear' }]}/>, document.getElementById('embedded')); 

Jordan and Felix helped me on to the right track. Jordan和Felix帮助我走上正轨。

  • I kept my files (Embedded.jsx, Prop.jsx) separated. 我将我的文件(Embedded.jsx,Prop.jsx)分开了。
  • I removed the last line from each file ReactDom.render(...) and put them into another file. 我从每个文件ReactDom.render(...)删除了最后一行,并将它们放入另一个文件中。
  • Went back to both files and on the last line of each, I just export default the class. 回到两个文件和每个文件的最后一行,我只export default类。

Now everything is working properly. 现在一切正常。

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

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