本文介绍了您必须将组件传递给connect返回的函数。而是收到undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码给出了

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);

PostList.js

import React from 'react'

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

请帮我解决一下?

推荐答案

你正在做从'../ components / PostList'导入PostList; 所以你需要使用 export default 。

You are doing import PostList from '../components/PostList'; so you need to use export default in your PostList.js file.

否则你需要做 import {PostList来自'../ components / PostList';

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

To whoever is interested, here is a nice article about es6 import/export syntax: http://www.2ality.com/2014/09/es6-modules-final.html

这篇关于您必须将组件传递给connect返回的函数。而是收到undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 15:28