简体   繁体   English

错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件)但得到:对象

[英]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.

相关问题 NextJS:元素类型无效:期望字符串(用于内置组件)或类/函数(用于复合组件)但得到:未定义 - NextJS: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined Next Js Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) 但得到的是:undefined - Next Js Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义 Next JS - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined Next JS 未捕获的错误:不变违规:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数但得到:object - Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function but got: object 错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件) - error:Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) 元素类型无效:应为字符串(用于内置组件)或类/函数。 使用 nodejs、react、react-router-dom 的 SSR - Element type is invalid: expected a string (for built-in components) or a class/function. SSR with nodejs, react, react-router-dom 如何修复React中的“元素类型无效:预期为字符串…但得到:对象”错误 - How to fix “Element type is invalid: expected a string … but got: object” error in React 尝试对服务器端进行渲染时,出现“元素类型无效:预期为字符串” - got “Element type is invalid: expected a string” when trying to server side rendering react class 组件 function 未找到 - class components function not found BABEL_TRANSFORM_ERROR。 属性值期望字符串类型但得到 object - BABEL_TRANSFORM_ERROR. Property value expected type of string but got object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM