Last active
October 22, 2021 06:28
-
-
Save VithuJey/fe5d5811375ff354b9cc18c20ec0e030 to your computer and use it in GitHub Desktop.
Just a gist to explain Redux Toolkit
This file contains hidden or 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, PayloadAction } from '@reduxjs/toolkit' | |
export interface OrderState { | |
item: string; | |
qty: number; | |
total: number; | |
} | |
const initialState: OrderState = { | |
item: "", | |
qty: 0, | |
total: 0, | |
} | |
export const orderSlice = createSlice({ | |
name: 'order', | |
initialState, | |
reducers: { | |
addItem: (state, action: PayloadAction<string>) => { | |
state.item = action.payload | |
}, | |
remItem: (state) => { | |
state.item = "" | |
}, | |
incrementQty: (state) => { | |
state.qty += 1 | |
}, | |
decrementQty: (state) => { | |
state.qty -= 1 | |
}, | |
incrementTotal: (state, action: PayloadAction<number>) => { | |
state.total += action.payload | |
}, | |
decrementTotal: (state, action: PayloadAction<number>) => { | |
state.total -= action.payload | |
}, | |
}, | |
}) | |
export const { addItem, remItem, incrementQty, decrementQty, incrementTotal } = orderSlice.actions | |
export default orderSlice.reducer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment