2016-12-03 16 views
0

還元剤を入れ子状態のプロパティに割り当てますか?下記の反応/ Reduxのセットアップを

import React from 'react'; 
import { render } from 'react-dom'; 
import { Provider } from 'react-redux'; 
import App from './components/app'; 
import configureStore from './store'; 

let initialState = { 
    foods: { 
    apple: { 
     selected: false, 
     flavors: { 
     sweet: true, 
     salty: false 
     } 
    }, 
    potatoChips: { 
     selected: false, 
     flavors: { 
     sweet: false, 
     salty: true 
     } 
    } 
    }, 
    drinks: { 
    < some other object > 
    } 
} 

let store = configureStore(initialState); 

render(
    <Provider store={store}> 
    <App/> 
    </Provider>, 
    document.getElementById('app') 
) 

store.js

import { applyMiddleware, compose, createStore } from 'redux'; 
import rootReducer from './root-reducer'; 
import logger from 'redux-logger'; 
import thunk from 'redux-thunk'; 

let finalCreateStore = compose(
    applyMiddleware(thunk, logger()) 
)(createStore) 

let defInitialState = < same object as initialState above > 

export default function configureStore(initialState = defInitialState){ 
    return finalCreateStore(rootReducer, initialState) 
} 

ルートreducer.js

import { combineReducers } from 'redux'; 
import appleReducer from './apple-reducer'; 
import potatoChipsReducer from './potato-chips-reducer'; 


const rootReducer = combineReducers({ 
    foods.apple: appleReducer, 
    foods['potatoChips']: potatoChipsReducer 
    // HOW DO I DO THE ABOVE ASSIGNMENT? 
}) 

export default rootReducer 

index.jsは私が割り当てることはでき私がやっているようにstate.foods.appleへの還元人上記の3210?それとも別のやり方でこれをやりますか?

はあなたが巣combineReducersでき

答えて

3

をextratextsothisquestionisntallcodeandcanbesubmitted。

例:https://jsfiddle.net/2rhs2m9y/

const foodsReducer = combineReducers({ 
    apple: appleReducer, 
    potatoChips: potatoChipsReducer, 
}) 

const rootReducer = combineReducers({ 
    foods: foodsReducer, 
}) 

だからあなたappleReducerは等々、その状態としてのみfoods.apple木を受け取り、かつます。

あなたは、代わりに(combineReducersがあなたのためにやっていること、本質的である)行うことができます:

const rootReducer = combineReducers({ 
    foods: (state, action) => ({ 
    apple: appleReducer(state.apple, action), 
    potatoChips: potatoChipsReducer(state.potatoChips, action), 
    }), 
}) 
+1

ありがとうございました!私が探していたもの – lsimmons

関連する問題