Created
February 19, 2020 22:31
-
-
Save Acetyld/1a76138902821356724afdf770a0bc90 to your computer and use it in GitHub Desktop.
This file contains 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 } 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