DEV Community

Discussion on: About my newly learned technology (Redux toolkit)

Collapse
 
markerikson profile image
Mark Erikson • Edited

Hi, I"m a Redux maintainer. Out of curiosity, any parts of our docs that you specifically feel "didn't click" for you? Any aspects of Redux you feel are particularly confusing?

FWIW, we would recommend against using separate action type string constants with Redux. That's part of the point of Redux Toolkit in the first place :) It still uses those inside, but you don't have to write them yourself.

Today a typical Redux reducer looks like this:

redux.js.org/tutorials/essentials/...

import { createSlice } from '@reduxjs/toolkit'

export const counterSlice = createSlice({
  name: 'counter',
  initialState: {
    value: 0
  },
  reducers: {
    increment: state => {
      // Redux Toolkit allows us to write "mutating" logic in reducers. 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
    }
  }
})

export const { increment, decrement, incrementByAmount } = counterSlice.actions

export default counterSlice.reducer
Enter fullscreen mode Exit fullscreen mode