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