Skip to content

Instantly share code, notes, and snippets.

@VithuJey
Last active October 22, 2021 06:28
Show Gist options
  • Save VithuJey/fe5d5811375ff354b9cc18c20ec0e030 to your computer and use it in GitHub Desktop.
Save VithuJey/fe5d5811375ff354b9cc18c20ec0e030 to your computer and use it in GitHub Desktop.
Just a gist to explain Redux Toolkit
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