简体   繁体   English

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

[英]React: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object

I am learning React and I faced this error我正在学习 React 并且遇到了这个错误

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件)但得到:对象。

I've checked all the previous questions related to similar errors but can't find out what's wrong.我已经检查了与类似错误相关的所有先前问题,但无法找出问题所在。 Here is my code:这是我的代码:

import React from 'react'
import classes from './BuildControls.css'
import BuildControl from './BuildControl/BuildControl.css'

const controls = [
    {label: 'Salad',type: 'salad'},
    {label: 'Bacon',type: 'bacon'},
    {label: 'Cheese',type: 'cheese'},
    {label: 'Meat',type: 'meat'},
];

const buildControls = (props) => (
    <div className={classes.buildControls}>
        {controls.map(ctrl => (
            <BuildControl 
                key={ctrl.label} 
                label={ctrl.label}
                added={() => props.ingredientAdded(ctrl.type)}
                 />
        ))}       
    </div>
);

export default buildControls;

this is the file which contains error.这是包含错误的文件。 Thanks in advance提前致谢

You are importing the BuildControl stylesheet as BuildControl and trying to use that to create the BuildControl element.您正在将 BuildControl样式表作为BuildControl导入并尝试使用它来创建 BuildControl 元素。 You need to instead import BuildControl from './BuildControl/BuildControl.jsx' (assuming that's your component's filename and location).您需要import BuildControl from './BuildControl/BuildControl.jsx' (假设这是您的组件的文件名和位置)。

To render a child component inside a parent component, you need to import the child component's JS file.要在父组件内渲染子组件,您需要导入子组件的 JS 文件。

As a side note, it's common practice to always use pascal case (eg, "BuildControls") when naming components.作为旁注,通常的做法是在命名组件时始终使用pascal 大小写(例如,“BuildControls”)。

暂无
暂无

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

相关问题 React-元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到:对象 - React - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object 反应错误:元素类型无效:期望使用字符串(对于内置组件)或类/函数(对于复合组件),但得到:对象 - React error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object React native,元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到:未定义 - React native, Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined React - 错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义 - React - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 反应错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义 - React Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义的 React - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined React 错误:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件)但得到:ReactJS 中的对象 - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object in ReactJS 元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到:对象。 ReactJS - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. ReactJS 未捕获的错误:元素类型无效:期望一个字符串(对于内置组件)或类/函数(对于复合组件)但得到:对象 - Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object 如何修复元素类型无效:期望一个字符串(对于内置组件)或类/函数(对于复合组件)但得到:对象 - How to fix Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM