2017-04-30 18 views
2

私はレデューサーから店を取得しようとしています。 reduxアーキテクチャーは、レデューサー間の共有をサポートしていないことがわかりました。 しかし、私の場合は本当に必要です。Redux:レデューサーからの保管方法

const initState = { 
    schemas: [], 
}; 


const myReducer = (state , action) => { 
    state = state || initState; 

    switch (action.type) { 
    case 'redux-form/CHANGE': 
     const schemaId = getStore().context.schema; 
     let modifier = state.schemas.get(schemaId); 
     modifier[action.key] = action.value; 

     return { 
     ...state 
     }; 
    } 
}; 

私のアプリの減速:先

const iceApp = combineReducers({ 
    form: formReducer, 
    context, 
    myReducer, 
}); 

感謝。

答えて

0

あなたは、あなたの状態のいずれかのレベルにまで減速機能を追加を検討することができます

const complexReducer = (state, action) { 
    const {context, myReducer} = state; 

    switch (action.type) { 
    case 'redux-form/CHANGE': 
     // do something with myReducer 

     return {context, myReducer}; 
    default: 
     return state; 
    } 
} 

// a simple utility function to call reducers in sequence on the same state 
function composeReducers(...reducers) { 
    return (state, action) => reducers.reduceRight((acc, reducer) => reducer(acc, action), state); 
} 

const iceApp = composeReducers(complexReducer, combineReducers({ 
    form: formReducer, 
    context, 
    myReducer, 
})); 

これは、単純な減速からの全体の状態にcomplexReducerを適用します。

異なるアプローチは、アクション内のcontextにアクセスし、アクションのペイロードとして渡すことです。

const changeAction = ... // the redux-form/change action 

const changeActionWithContext =() => (dispatch, getState) => { 
    const state = getState(); 
    const schemaId = state.context.schema; 

    dispatch(changeAction(schemaId)); 
} 

私は今redux-formので、私はそれが可能だかどうかわからないしていないか、redux-formアクションにカスタムペイロードを追加することではありません。

+0

ありがとうございました。 –

関連する問題