You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

Redux connect报错:传入undefined而非组件,求解容器/展示组件连接问题

Fixing the Redux Error: "You must pass a component to the function returned by connect. Instead received undefined"

Hey there, let's work through this frustrating Redux error you're facing. This message almost always boils down to a small mix-up in how you're importing your components or using the connect higher-order component. Let's break down the most likely fixes based on what you've shared:

1. Double-Check Your Component Imports

Looking at your code snippet, you have import TodoFormAdd from '../../Containers/...—this is a common spot for mistakes:

  • Path accuracy: Make sure the relative path to your container component is correct. A missing or misnamed directory/file here will result in importing undefined instead of your component.
  • Export/import matching: If your container component uses a default export (like export default connect(...)), your import should not use curly braces. If it uses a named export (like export const TodoFormAdd = connect(...)), you must wrap the component name in curly braces (import { TodoFormAdd } from ...).

Example of correct default export/import:

// Container component file
import { connect } from 'react-redux';
import TodoFormAdd from '../components/TodoFormAdd';
import AddTodo from '../../Actions/AddTodo';

const mapDispatchToProps = { addTodo: AddTodo };

export default connect(null, mapDispatchToProps)(TodoFormAdd); // Default export
// Where you import the container
import TodoFormAdd from '../../Containers/TodoFormAdd'; // No curly braces

2. Verify the connect Call Order

The connect function has a strict order: first pass your mapStateToProps and mapDispatchToProps (or null if you don't need one), then pass your React component. Mixing this up will cause Redux to receive undefined instead of a component.

❌ Wrong order:

// Don't do this!
connect(TodoFormAdd)(null, mapDispatchToProps);

✅ Correct order:

connect(null, mapDispatchToProps)(TodoFormAdd);

3. Ensure Your Component Is Properly Defined & Exported

Make sure your presentational component (the one you're connecting to Redux) is a valid React component and has an export. For example:

// Presentational component (TodoFormAdd.js)
import React from 'react';

const TodoFormAdd = () => {
  return (
    <form>
      {/* Your form content here */}
    </form>
  );
};

export default TodoFormAdd; // Critical: don't forget this export

4. Check for Circular Imports

If your presentational component imports something from the container component, and the container imports the presentational component, you'll create a circular dependency. This can lead to one component being undefined when imported. Fix this by refactoring your imports to break the cycle (e.g., move shared logic to a separate file).

Start with the first two checks—they're the most common culprits for this error. Once you fix those, your component should connect to Redux without issues.

内容的提问来源于stack exchange,提问作者Max Wolfen

火山引擎 最新活动