I have a login modal. I use an action creator to turn isLoginModalVisible: true in the reducers. Calling the action creator inside a container, inside a component's onClick, like so:
<TextButtonRight onClick={()=> this.props.showLoginModal(true)}>
Inside the login modal, I have a login button that submits a form. I want the modal to disappear when 200OK otherwise, I display an error message in the modal. calling this action creator on login button click:
export function logInUser(){
console.log("Actions--> logInUser");
//Imagine I got a 200OK
dispatch(showLoginModal(false));
return({
type: UPDATE_USER_LOGGED_IN,
payload: true
});
}
I'm calling the action creator to hide the modal from logInUser action creator and it's not working. The showLoginModal action creator does get hit but it does not dispatch to a reducer.
Here's the showLoginModal action creator:
export function showLoginModal(bool){
console.log("Actions--> showLoginModal:"+bool);
return({
type: UPDATE_LOGIN_MODAL_VISIBLE,
payload: bool
});
}
(I'm sorry if I'm making a rookie mistake)
Edit:
part of my reducer.It does work because that's how I show the login modal. I pass false when I want to hide it. There's no problem with the payload either. The console.log isn't getting get when I call it via another action creator. works okay when I pass it to onClick on submit button this way:
onClick={()=> this.props.showLoginModal(false)}
case UPDATE_LOGIN_MODAL_VISIBLE:
console.log("REDUCER-->"+UPDATE_LOGIN_MODAL_VISIBLE);
return {...state, loginModalVisible: action.payload};
break;
CombineReducers:
const rootReducer = combineReducers({
posts: postsReducer,
displayComps: displayComponentsReducer,
form: formReducer,
searchForm: searchFormReducer
});
Edit 2:
Sorry, I should have mentioned that I am using thunk. I wanted to know if there was a way to call an action from this one, like so:
req.then((response)=>{
console.log("REQ_COMPLETE");
dispatch({type: UPDATE_USER_LOGGED_IN, payload: true});
//like so:
showLoginModal(false);
localStorage.setItem('userLoggedIn', true);
})
instead of adding this:
dispatch({type: UPDATE_LOGIN_MODAL_VISIBLE, payload: false});
dispatching the action from another action creator seemed kinda hacky.