Last active
March 2, 2020 16:58
-
-
Save bsdis/cdf772bda24a5b2b05817a8c0a2abb48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createSlice, configureStore } from "@reduxjs/toolkit" | |
import { createStore, combineReducers } from "redux" | |
import { INITIAL_STATE } from "./state" | |
const basketSlice = createSlice({ | |
name: "basket", | |
initialState: INITIAL_STATE, | |
reducers: { | |
add: (state, action) => { | |
// add item to basket using `state` and `action` props | |
return state.map(item => { | |
if (item.id !== action.payload.id) { | |
return item | |
} | |
return { | |
...item, | |
added: true | |
} | |
}) | |
}, | |
remove: (state, action) => { | |
return state.map(item => { | |
if (item.id !== action.payload.id) { | |
return item | |
} | |
return { | |
...item, | |
added: false | |
} | |
}) | |
} | |
} | |
}) | |
const reducer = combineReducers({ | |
foo: basketSlice | |
}) | |
const store = configureStore({ reducer: basketSlice.reducer }) | |
export const { add, remove } = basketSlice.actions | |
export { basketSlice, store } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment