import { createSlice } from '@reduxjs/toolkit';
import type { PayloadAction } from '@reduxjs/toolkit';
// Define the shape of the counter state
interface CounterState {
  value: number;
}
// Initial state
const initialState: CounterState = {
  value: 0,
};
// Create a slice for the counter
const counterSlice = createSlice({
  name: 'counter',
  initialState,
  reducers: {
    increment: (state) => {
      state.value += 1;
    },
    decrement: (state) => {
      state.value -= 1;
    },
    // Optional: Action with payload
    incrementByAmount: (state, action: PayloadAction<number>) => {
      state.value += action.payload;
    },
  },
});
// Export actions
export const { increment, decrement, incrementByAmount } = counterSlice.actions;
// Export reducer
export default counterSlice.reducer;
Top