So I was writing some code for react-redux and I encounter this anomalous behaviour in javascript. Below is the code.
case CONSTANTS.ADD_CARD: {
const { listId, card } = action.payload;
if(typeof state[listId]==="undefined"){
let newCard = [...state[listId], card] //defined variable based on condition
}else{
let newCard = [card] //defined variable for fail condition
}
const newState = {
...state
}
newState[listId] = newCard //error undefined variable showing hear
return newState;
}
The above code is a part of switch statement.
Also, I tried declaring a variable above if/else block and then initiated it in the if/else block, but encountered the same error.
I solved the problem using the ternary operator, but still wondering the reason for the problem. Please let me know the reason behind this.
Thank you
Aucun commentaire:
Enregistrer un commentaire