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
}
}
}