【问题标题】:Action not being passed to the reducer - Redux操作未传递给减速器 - Redux
【发布时间】:2016-04-13 10:17:24
【问题描述】:

我的 react/redux 应用程序有问题,特定操作 buildModalForm() 没有从 actions/index/ 传递到减速器。我对反应还很陌生,所以不确定问题是什么。

在容器/buidpage中:

    class BuildPage extends Component {

   componentWillMount(){
     this.props.fetchAPIJobs();
     this.props.buildModalForm();
   }

//....


const mapStateToProps = (state) => {

    return {
      rowData  : state.buildJobs.rowData,
      columns  : state.buildJobs.columns,
      reactView: state.reactView
      }
 }


const mapDispatchToProps = (dispatch) => {
    return {
        onFormClick: () => {
            dispatch( showModal() )
        },
        onModalClose: () => {
            dispatch( hideModal() )
        },
        buildModalForm : () => {
          dispatch( buildModalForm() )
        },
        fetchAPIJobs : () => {
          fetchAPIJobs(fetchAPIJobs())
        }
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(BuildPage)

在动作/索引中:

    import * as actionTypes from '../constants/actionTypes'

export function buildModalForm(){
  console.log("in action creator buildModalForm");
  return { type: actionTypes.BUILD_MODAL_FORM }
}

在常量/动作类型中:

export const BUILD_MODAL_FORM = 'BUILD_MODAL_FORM'

在reducers/reactView中:(连接到根reducer)

 import {
  HIDE_MODAL,
  SHOW_MODAL,
  BUILD_MODAL_FORM
} from '../constants/actionTypes.js'

import {
  BUILD_INITIAL_STATE,
  ROOT_INITIAL_STATE
} from '../constants/initialStates'


export default function reactView(state = {}, action) {
  console.log("in react view reducer :", action.type);
  switch (action.type){
    case SHOW_MODAL :
      return Object.assign( {}, state, {modalDisplay: true})
    case HIDE_MODAL :
      return Object.assign( {}, state, {modalDisplay: false})
    case BUILD_MODAL_FORM :
      return Object.assign( {}, state, BUILD_INITIAL_STATE)
    default :
      return state
  }
}

actions/index 中的console.log 记录了它在action 内部,reducer 内部的日志正在读取其他action 类型,但BUILD_MODAL_FORM 没有通过被记录。任何帮助我表示赞赏。

【问题讨论】:

  • 你还有其他的减速机吗?你确定他们不会改变状态吗?
  • @VonD 是的,我还有其他一些 reducer,但都使用 Object.assign,类似于此处所示。我的印象是 Object.assign 不会改变状态,但也许这是不对的?
  • 这取决于Object.assign({}, state, newData),就像您在这里所做的那样,不是。但是Object.assign(state, newData) 会。
  • 啊,是的,我总是将第一个参数作为空对象。不过,这对未来很有用。
  • 实际上愚蠢的错误是中间件有错字。我会关闭这个。

标签: javascript reactjs redux


【解决方案1】:

-- 我打错了,没有从中间件返回任何东西。结案了,呵呵。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 2020-07-26
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    相关资源
    最近更新 更多