JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.6.0/redux.min.js"></script>
<div id="result"></div>
JavaScript
const {
combineReducers,
createStore
} = Redux
const TOGGLE_APPLE_SELECT = 'TOGGLE_APPLE_SELECT'
const appleReducer = (state = {}, action) => {
switch (action.type) {
case TOGGLE_APPLE_SELECT:
return Object.assign({}, state, {
selected: !state.selected,
})
default:
return state
}
}
const potatoChipsReducer = (state = {}, action) => state
const foodsReducer = combineReducers({
apple: appleReducer,
potatoChips: potatoChipsReducer,
})
const rootReducer = combineReducers({
foods: foodsReducer,
})
const initialState = {
foods: {
apple: {
selected: false,
flavors: {
sweet: true,
salty: false
}
},
potatoChips: {
selected: false,
flavors: {
sweet: false,
salty: true
}
}
}
}
const store = createStore(rootReducer, initialState)
store.dispatch({
type: TOGGLE_APPLE_SELECT
})
document.getElementById('result').innerHTML = '<pre>' + JSON.stringify(store.getState(), null, ' ') + '</pre>'