Reactjs 导入redux操作会使其他操作未定义

Reactjs 导入redux操作会使其他操作未定义,reactjs,redux,Reactjs,Redux,这是我见过的最奇怪的事情之一。这对我来说毫无意义。简短的版本是我有一个Redux action creator函数。如果我将这个函数导入到这个特定的组件文件中,它会使从其文件导入的每个函数都未定义 那么,让我们从文件filterInputModal.actions.js开始。这包含使用Redux初学者工具包创建的我的Redux操作函数: export const showAddCategoryModal = createAction('showAddCategoryModal'); 这就是我一

这是我见过的最奇怪的事情之一。这对我来说毫无意义。简短的版本是我有一个Redux action creator函数。如果我将这个函数导入到这个特定的组件文件中,它会使从其文件导入的每个函数都未定义

那么,让我们从文件filterInputModal.actions.js开始。这包含使用Redux初学者工具包创建的我的Redux操作函数:

export const showAddCategoryModal = createAction('showAddCategoryModal');
这就是我一直在使用的函数。现在,该函数早已导入到我的ManageVideoFilters.js组件中:

import React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { showAddCategoryModal } from 'store/filterInputModal/filterInputModal.actions';

const ManageVideoFilters = (props) => {
    /* Component logic */
};

/* PropTypes and mapStateToProps */

const mapDispatchToProps = (dispatch) => bindActionCreators({
    showAddCategoryModal: () => showAddCategoryModal() // Done this way to avoid passing in a payload, since certain default event payloads cause Redux to print console errors
});

export default connect(mapStateToProps, mapDispatchToProps)(ManageVideoFilters);
到目前为止还不错。在我们去打破一切之前,让我们来看看我的FieldInPotoMald.Roope.JS ReDux减速器,也使用ReDux启动器套件创建:

import { createReducer } from 'redux-starter-kit';
import { showAddCategoryModal } from './filterInputModal.actions';

const initialState = {}; // The initial state for the reducer goes here

const handleShowAddCategoryModal = (state) => {
    /* handle updating the state */
    return state;
};

const actionMap = {
    [showAddCategoryModal]: handleShowAddCategoryModal
};

export default createReducer(initialState, actionMap);
动作映射使用动作创建者函数toString()作为键,然后我提供自己的函数来处理状态更新。同样,在这一点上,一切都是完美的。我们马上回到减速机,首先让我们打破它

现在我们来看看我的VideFileEdit.js组件。如果我们将以下行添加到此组件,则所有内容都将中断:

import { showAddCategoryModal } from 'store/filterInputModal/filterInputModal.actions';
那么,它是如何断裂的呢

  • 现在未定义filterInputModal.reducer.js中ShowAddCategoryModel函数的导入
  • 由于reducer将函数用作处理操作的键,因此reducer不再能够正确处理操作并更新状态
  • 但情况变得更奇怪了。以下是我看到的一些奇怪的行为

  • 如果我将此操作导入任何其他组件,则一切正常。减速器中的进口保持不变
  • 在ManageVideoFilters.js和VideoFileEdit.js中导入该函数都可以

  • 那么,我下一步可以尝试什么?这真的很奇怪,对我来说没有任何意义。我以前从未见过这种情况。

    正如评论员所说,问题在于递归导入。我的filterInputModal.reducer.js导出了一些常量,这些常量被导入到我的filterInputModal.actions.js中。然后将filterInputModal.actions.js中的操作导入filterInputModal.reducer.js。因此需要递归导入


    我将常量移动到一个新文件filterInputModal.constants.js中,问题解决了。您的文件中有递归导入吗?