Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
Reducer add item and update item in array of objects without mutation

Hello created reducer and wondering is this right pattern.

I am trying to:

  1. Add new item into array of objects ACTION_TYPE
  2. Update item in array of objects ACTION_TYPE_EDIT, this one actually should set charity prop and update item in array charities

Wondering if i am on right path, i don't want to mutate, so here is my code.

export const reducerCharities = (
  state: CharityReducerState,
  action: CharitiesAction
    | CharityEditAction
) => {
  switch (action.type) {
    case ACTION_TYPE_EDIT:
      return {
        ...state,
        charity: {
          ...state.charity,
          ...(action.charity || {}),
        },
        charities: [
          ...state.charities.map(item => item.id === action.charity?.id ? { ...item, ...action.charity } : item)
        ]
      }
    case ACTION_TYPE:
      return {
        ...state,
        charities: [
          ...state.charities || [],
          ...action.charities
        ]
      }
    default:
      return state
  }
}

So my question is will this part mutate state or is there better way to write this?

charities: [
          ...state.charities.map(item => item.id === action.charity?.id ? { ...item, ...action.charity } : item)
        ]

Is this right approach how to add new item into array

     {
        ...state,
        charities: [
          ...state.charities || [],
          ...action.charities
        ]
      }
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

The easiest (and since 2019 officially recommended) way of doing this would be using the official Redux Toolkit package and the createSlice method.

Within that, you can just mutate and don't have to care about manual immutable updates - the package takes over for you.

The code could look like

const charitiesSlice = ({
  name: 'charities',
  initialStae: myInitialState,
  reducers: {
    edit(state, action: PayloadAction<Charity>){
      const charity = state.charities.find(item => item.id === action.payload.id)
      if (charity) {
        Object.assign(charity, action.payload)
      }
      state.charity = action.payload
    }
  }
}
 
export const { edit } = charitiesSlice.actions
export const reducerCharities = charitiesSlice.reducer
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!