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

93
Views
How to add values to nested state array in reducer payload?

I'm trying to add values from a response into my Redux state in the reducer. I have a state object that looks like this:

state: {
  currentUser: {
    loggedIn: boolean,
    follows: [string array],
    following: [string array],
    uid: string
  }
}

There are others, but this shows the shape that I need to add to. When a follow is added from a response, I need to add it to the following array. When I try to access it, it says it 'could possibly be null.' Is there something I need to be doing? I'm specifically looking at ADD_FOLLOW action.

import actions from '../actions/userActions'

const initialState = {
  loggedIn: false,
  currentUser: null
}

export const userReducer = (state: any = initialState, action: any) => {
  switch (action.type) {
    // User logic will go here
    case actions.LOG_IN: {
      console.log('user logged in! Action obj: ', action)
      sessionStorage.setItem('user_object', JSON.stringify(action.payload))
      return {
        ...state,
        loggedIn: true,
        currentUser: {
          ...action.payload,
          uid: action.payload._id
        }
      }
    }

    case actions.LOG_OUT: {
      sessionStorage.clear()
      return initialState
    }

    case actions.REAUTH: {
      return state
    }

    case actions.UPDATE_INFO: {
      console.log('payload for update user: ', action.payload)
      return {
        ...state,
        currentUser: {
          ...action.payload
        }
      }
    }

    case actions.ADD_FOLLOW: {
      return {
        ...state,
        currentUser: {
          follows: [
            ...state.currentUser.follows || [],
            action.payload.resource_id
          ]
        }
      }
    }

    default: {
      return state
    }
  }
}
about 4 years ago · Juan Pablo Isaza
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!