2017-02-19 42 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()改爲。