Skip to content

Instantly share code, notes, and snippets.

@VithuJey
Last active Oct 22, 2021
Embed
What would you like to do?
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