I am very new to React and come across useReducer. If I paste my code here, it will be very long, because it involve component, useContext and useReducer.So I will paste some of the code only. I have question about the state in try and catch loop. My reducer fire up a login method that use firebase signInWithEmailAndPassword(). Am I doing the try and catch loop wrong? I am able to see the error message in the console.log, but my state won't update with the error message, neither in the then().catch() nor in the try and catch loop.
const loginUser = (usename, password, state) => {
try{
const res = auth.signInWithEmailAndPassword(email, password).then( user => {
return {...state, userid: user.uid}
}).catch(error => {
return {...state, error: error.message}
})
}catch (error) {
console.log("oops !! error: ", error);
return {...state, error: error, isLoggedIn:false, isLoading:false};
}
}
export const storeReducer = (state, action) => {
switch (action.type) {
case USER_LOGIN:
return loginUser(action.username, action.password, state);
default:
return state;
}
};
The code above is a simplified version that I am facing problem with updating the state using useReducer.