简体   繁体   English

您必须将一个组件传递给 connect 返回的函数。 而是收到未定义

[英]You must pass a component to the function returned by connect. Instead received undefined

The code below gives下面的代码给出

Uncaught Error: You must pass a component to the function returned by connect.未捕获的错误:您必须将一个组件传递给 connect 返回的函数。 Instead received undefined而是收到未定义

List.js列表.js

import React from 'react';
import { connect, bindActionCreators } from 'react-redux';
import PostList from '../components/PostList'; // Component I wish to wrap with actions and state
import postList from '../Actions/PostList' //Action Creator defined by me

const mapStateToProps = (state, ownProps) => {
    return state.postsList
}

const mapDispatchToProps = (dispatch) => {
    return bindActionCreators({"postsList":postList},dispatch)
}

export default connect(mapStateToProps, mapDispatchToProps)(PostList);

PostList.js PostList.js

import React from 'react'

export const PostList = (props) => {
    return <div>List</div>
}

Please help me with a solution?请帮我解决一下?

You are doing import PostList from '../components/PostList';您正在import PostList from '../components/PostList'; so you need to use export default in your PostList.js file.所以你需要在 PostList.js 文件中使用export default

Otherwise you need to do import { PostList } from '../components/PostList';否则你需要做import { PostList } from '../components/PostList'; . .

To whoever is interested, here is a nice article about es6 import/export syntax: http://www.2ality.com/2014/09/es6-modules-final.html对于感兴趣的人,这里有一篇关于 es6 导入/导出语法的好文章: http ://www.2ality.com/2014/09/es6-modules-final.html

Not related to the asker specifically, but if you're facing this error, it's worth to check if you have the connect() syntax right:与提问者无关,但如果您遇到此错误,则值得检查您的 connect() 语法是否正确:

const PreloadConnect = connect(mapStateToProps, {})(Preload);

export default PreloadConnect;

Note that Preload , is passed as a IIFE parameter.请注意, Preload是作为 IIFE 参数传递的。

More details can be found here .可以在此处找到更多详细信息。

There might be three reasons, that are summarized as follows:可能有以下三个原因,总结如下:

  • Circular dependencies between components组件之间的循环依赖
  • Wrong usage of export and export default then imported the wrong way错误使用exportexport default然后以错误的方式导入
  • Used the connect function wrongly, passed the wrong parameters错误地使用了connect函数,传递了错误的参数

In my case is was Circular dependencies, and the circular-dependency-plugin helped me fix it.在我的情况下是循环依赖,循环依赖插件帮助我修复了它。

In my case it was Expo server that sometimes doesn't catch filesaves on Windows (probably) and it was seening old version of the component I've tried to connect (I had no export there yet probably).在我的情况下,Expo 服务器有时无法捕获 Windows 上的文件保存(可能),并且它看到了我尝试连接的组件的旧版本(我可能还没有导出)。 Re-saving my component without really touching anything fixed the issue.在没有真正触及任何东西的情况下重新保存我的组件解决了这个问题。

Restarting Expo server with cleaned cache would probably help as well.使用清理过的缓存重新启动 Expo 服务器也可能会有所帮助。

In my case, it was because of the usage of enums (TypeScript).就我而言,这是因为使用了枚举(TypeScript)。 Try without enums in your code.尝试在您的代码中不使用枚举。

Reason : Enums can go undefined during runtime.原因:枚举在运行时可能未定义。

Link to Related Question 相关问题的链接

Hope it solves your problem :)希望它能解决您的问题:)

暂无
暂无

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

相关问题 如何修复错误:“您必须将组件传递给连接返回的 function。 而是在我的 React-Redux 应用程序中收到 {}”? - How to fixed error: “You must pass a component to the function returned by connect. Instead received {}”, in my React-Redux app? ReactJS您必须将组件传递给connect返回的函数 - ReactJS You must pass a component to the function returned by connect 应用程序有效,但会产生错误:Connect(App)中的mapDispatchToProps()必须返回一个普通对象。 而是收到未定义 - Aplication works but generates an error: mapDispatchToProps() in Connect(App) must return a plain object. Instead received undefined 不变违规:组件`RNCSafeAreaProvider` 的查看配置getter 回调必须是一个函数(收到`undefined`) - Invariant Violation: View config getter callback for component `RNCSafeAreaProvider` must be a function (received `undefined`) 不变违规:查看组件“RecentExpenses”的配置 getter 回调必须是 function(接收到“undefined”) - Invariant Violation: View config getter callback for component `RecentExpenses` must be a function (received `undefined`) 反应本机 - 组件“输入”的视图配置 getter 回调必须是 function(收到“未定义”) - React native - view config getter callback for component 'input' must be a function (received 'undefined') 不变违规:组件“div”的视图配置 getter 回调必须是 function(收到“未定义”)。 确保启动组件 - Invariant Violation: View config getter callback for component `div` must be a function (received `undefined`). Make sure to start componen React native `input` 必须是 function 未定义 - React native `input` must be a function received undefined React-Redux容器在Connect(ModalRoot)中抛出“ mapStateToProps()”时必须返回一个普通对象。 而是收到未定义的信息。” - React-Redux container throws “mapStateToProps() in Connect(ModalRoot) must return a plain object. Instead received undefined.” 组件“div”的视图配置 getter 回调必须是 function(收到“未定义”)。 确保以大写字母开头的组件名称 - view config getter callback for component 'div' must be a function (received 'undefined'). Make sure to start component names with a capital letter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM