2017-02-19 38 views
0

我站在一个棘手的情况。减速器未点火

我我的减速器rhythmReducer.js如下:

import {TOGGLE_NOTE_VALUE} from '../constants/actionTypes'; 
import objectAssign from 'object-assign'; 
import initialState from './initialState'; 

export default function rhythmReducer(state = initialState.rhythm, action) { 
    let newState = objectAssign({}, state); 
    console.log("---RhythmReducer"); 
    console.log(action.type); 
    switch (action.type) { 
    case TOGGLE_NOTE_VALUE: 
    console.log("TOGGLE_NOTE_VALUE"); 
    return newState; 
    default: 
    return newState; 
    } 
} 

组件使用它是RhythmContainer.js:

import React, {PropTypes} from 'react'; 
import {connect} from 'react-redux'; 
import {bindActionCreators} from 'redux'; 
import * as actions from '../actions/rhythmActions'; 
import {Meter} from './Meter'; 

export const RhythmContainer = (props) => { 
    let rows = []; 
    for (let i=0; i < props.rhythm.meters.length; i++) { 
    rows.push(<Meter key={i} actions={actions} rhythm=  {props.rhythm.meters[i]}/>); 
    } 
    const handleClick =() => { 
    return props.store.dispatch(actions.toggleNoteValue); 
    }; 
    return (
    <div onClick={handleClick}> 
     This will be a 4/4 rhythm 
     {rows} 
    </div> 
); 
}; 

RhythmContainer.propTypes = { 
    rhythm: PropTypes.object.isRequired, 
    store: PropTypes.object.isRequired, 
}; 

function mapStateToProps(state) { 
    return { 
    rhythm: state.rhythm, 
    }; 
} 

function mapDispatchToProps(dispatch) { 
    return { 
    actions: bindActionCreators(actions, dispatch) 
    }; 
} 

export default connect(
    mapStateToProps, 
    mapDispatchToProps 

)(RhythmContainer);

我的动作在rhythmActions.js

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

export function toggleNoteValue() { 
    console.log("toggleNoteValue"); 
    return {type: types.TOGGLE_NOTE_VALUE}; 
} 

定义即使减速运行,当页面正在初始化,我不能得到它,当我点击DIV运行。 toggleNoteValue()正在启动,但它永远不会在实际的Reducer中执行。 有什么帮助吗?

PS整个项目是在这里以防万一它有助于:https://github.com/ichionid/rhythmGeneratorReact/tree/master/src

+0

您没有将'Note'组件与redux连接起来。 –

+0

另外,您没有通过dispatch()发送操作。 – macbem

+0

我传播的功能下降到元素,但我有3包装组件,这将导致帖子不可读。你通过派遣@macbem表达什么意思?我有函数mapDispatchToProps(dispatch){ return { actions:bindActionCreators(actions,dispatch) }; }在我的主要组件 –

回答

2

这里有几件事情要试试。

  • 在项目中,从configureStore.js "../rootReducer"导入一个rootReducer,但有没有这样的模块。我不确定这是否是 只是一个提交问题,但它值得检查。

  • 调度的理由应该是一个动作。 actions.toggleNoteValue 不是一个动作,它是一个返回动作的函数。试试
    props.store.dispatch(actions.toggleNoteValue())
    props.actions.toggleNoteValue()改为。