[英]Error:Element type is invalid: expected a string (for built-in components)or a class/function(for composite components)but got:object
import React from 'react'
const Newslist=(props)=>{
const Items = props.news.map((item)=>{
return (<h2>{item.title}</h2> )
});
return(<div> <Items/> </div>)
}
export default Newslist;
This piece of code is not displaying anything in the dom and showing the error这段代码没有在dom中显示任何内容并显示错误
Error:Element type is invalid: expected a string (for built-in components)or a class/function(for composite components)but got:object
错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件)但得到:对象
You're rendering Items
as if it were a React component and not a JSX variable.您将
Items
渲染为 React 组件而不是 JSX 变量。 As the error says, React components can only be created from strings (in the case of HTML elements), classes or functions.正如错误所说,React 组件只能从字符串(在 HTML 元素的情况下)、类或函数创建。 When you render something using the
<JSX/>
syntax it is passed as an argument to React.createElement
.当您使用
<JSX/>
语法渲染某些内容时,它会作为参数传递给React.createElement
。 An array of JSX elements, which is what your map
call returns, is not one of the accepted parameter types, so you are getting this error.一个 JSX 元素数组(您的
map
调用返回的内容)不是可接受的参数类型之一,因此您会收到此错误。 I think this should work:我认为这应该有效:
import React from 'react'
const Newslist=(props)=>{
const items = props.news.map((item)=>{
return (<h2>{item.title}</h2> )
});
return(<div> {items} </div>)
}
export default Newslist;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.