You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React项目报错:Element type is invalid问题求助

Fixing "Element type is invalid: expected a string or class/function but got: object" in React

Hey there, let's tackle this super common React error together! The message you're seeing almost always boils down to mismatched component imports/exports—exactly what the hint mentions. Since you've shared the relevant files (ArticleAll.js, ArticleCard.js, Routes.js, Navigation.js), let's walk through each possible culprit step by step.

1. Double-check Component Exports

First, make sure your components are exported correctly. React has two main export types, and mixing them up is the #1 cause of this error:

  • Default Exports: If you used export default ArticleCard in ArticleCard.js, your import needs to omit curly braces: import ArticleCard from './ArticleCard'.
  • Named Exports: If you used export const ArticleCard = () => {...}, your import must include curly braces: import { ArticleCard } from './ArticleCard'.

Start with ArticleAll.js and ArticleCard.js—this is where the issue usually lives. For example, exporting a component as default but trying to import it as a named export will make React receive an object instead of the component function/class.

2. Verify Imports in Routes & Navigation Files

Next, check how you're importing components in Routes.js and Navigation.js. Let's say you have:

import { ArticleAll } from './ArticleAll'

But ArticleAll uses a default export—this will break immediately. Typos in component names or file paths can also lead to this error (since importing a non-existent file/export often returns an empty object).

Also, make sure you're not accidentally importing a non-component object. For example, if a file exports both a component and some helper data, and you grabbed the data instead of the component by mistake.

3. Check Nested Component Imports

If ArticleAll renders ArticleCard, double-check that import inside ArticleAll. A super easy mistake to make is:

// Wrong: Using curly braces for a default export
import { ArticleCard } from './ArticleCard'

// Correct: No curly braces for default
import ArticleCard from './ArticleCard'

Or the reverse if ArticleCard uses a named export.

4. Quick Debugging Hack

If you're still stuck, add a console log right before you use the component to see what you're actually importing. For example, in ArticleAll.js:

import ArticleCard from './ArticleCard'
console.log('ArticleCard:', ArticleCard) // Should log a function/class, not an object

If it logs something like { default: [Function] }, that confirms you mixed up default and named imports.

Once you fix the import/export mismatch, this error should vanish. If you share snippets of your code, we can pinpoint the exact issue even faster!

内容的提问来源于stack exchange,提问作者Ardy Febriansyah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:12:54