Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

87
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda