Skip to content

Instantly share code, notes, and snippets.

@bsdis
Last active March 2, 2020 16:58
Show Gist options
  • Save bsdis/cdf772bda24a5b2b05817a8c0a2abb48 to your computer and use it in GitHub Desktop.
Save bsdis/cdf772bda24a5b2b05817a8c0a2abb48 to your computer and use it in GitHub Desktop.
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