Javascript 必须将组件传递给connect返回的函数。相反,接收到的是未定义的

Javascript 必须将组件传递给connect返回的函数。相反,接收到的是未定义的,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,下面的代码给出了 未捕获错误:必须将组件传递给connect返回的函数。相反,接收到的是未定义的 List.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 po

下面的代码给出了

未捕获错误:必须将组件传递给connect返回的函数。相反,接收到的是未定义的

List.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);
import React from 'react'

export const PostList = (props) => {
    return <div>List</div>
}
PostList.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);
import React from 'react'

export const PostList = (props) => {
    return <div>List</div>
}
从“React”导入React
导出常量PostList=(道具)=>{
返回列表
}

请帮助我解决问题?

您正在执行从“../components/PostList”导入PostList的
操作
因此您需要在PostList.js文件中使用
导出默认值

否则您需要从“../components/PostList”执行
import{PostList}


对于任何感兴趣的人,这里有一篇关于es6导入/导出语法的好文章:

与询问者无关,但如果您遇到此错误,请检查您是否正确使用了connect()语法:

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

export default PreloadConnect;
请注意,预加载作为IIFE参数传递。

可以找到更多详细信息

可能有三个原因,总结如下:

  • 组件之间的循环依赖关系
  • 错误使用
    导出
    导出默认值
    ,然后以错误的方式导入
  • 错误地使用了connect函数,传递了错误的参数

在我的例子中,是循环依赖关系,它帮助我修复了它。

在我的例子中,是Expo server有时无法在Windows上捕获文件存储(可能),而且它看起来是我尝试连接的组件的旧版本(我可能还没有在那里导出)。我们在不接触任何东西的情况下保存了我的组件,解决了问题


用清理过的缓存重新启动Expo server可能也会有帮助。

在我的例子中,这是因为使用了enum(TypeScript)。 尝试在代码中不使用枚举

原因:枚举可能在运行时未定义


希望它能解决您的问题:)

检查Postlist导入Postlist导入没有问题您确定不必从“../components/Postlist”导出默认值或导入{Postlist};赞成。我很确定以这种方式出口足以进口到其他任何地方(我不太确定)在我的例子中,我从另一个组件复制了代码,并更改了
propTypes
声明中除名称以外的所有内容