[英]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
andexport default
then imported the wrong way错误使用
export
和export 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.