Skip to content

Instantly share code, notes, and snippets.

@Acetyld
Created February 19, 2020 22:31
Show Gist options
  • Save Acetyld/1a76138902821356724afdf770a0bc90 to your computer and use it in GitHub Desktop.
Save Acetyld/1a76138902821356724afdf770a0bc90 to your computer and use it in GitHub Desktop.
import { createSlice } from '@reduxjs/toolkit';
export const slice = createSlice({
name: 'counter',
initialState: {
value: 0,
},
reducers: {
increment: state => {
// Redux Toolkit allows us to 'mutate' the state. It doesn't actually
// mutate the state because it uses the immer library, which detects
// changes to a "draft state" and produces a brand new immutable state
// based off those changes
state.value += 1;
},
decrement: state => {
state.value -= 1;
},
incrementByAmount: (state, action) => {
state.value += action.payload.amount;
},
},
});
export const selectCount = state => state.counter.value;
export const { increment, decrement, incrementByAmount } = slice.actions;
export default slice.reducer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment