I have a function that returns a redux thunk async function. The problem is it isn't firing. The createUser function is running because the first console.log statement inside it is running but the one inside the redux thunk function is not. I'm not entirely sure why? Any ideas for things I can try? This same good was working on my react-native app so not sure why it's not working here.
Here is the createUser function.
export function createUser (userData) {
console.log('Getting here?')
return function (dispatch, getState) {
dispatch(authenticating());
console.log('But are you getting heeeere?')
const email = userData.email;
const password = userData.password;
firebaseAuth.createUserWithEmailAndPassword(email, password).catch((error) => {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorMessage);
}).then(() => {
const user = firebaseAuth.currentUser;
db.ref('/users/' + user.uid).set({
username: userData.username,
displayName: userData.displayName,
uid: user.uid
})
dispatch(isAuthed());
}).catch((error) => {
console.warn('Error in createUser callback', error)
});
}
}
And my setup
import React from 'react';
import ReactDOM from 'react-dom';
import { createStore, applyMiddleware, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
import { App } from '~/components/App';
import registerServiceWorker from './registerServiceWorker';
import * as reducers from '~/redux';
import './index.css';
const store = createStore(
combineReducers(reducers),
applyMiddleware(thunk)
)
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root'));
registerServiceWorker();
Finally, here is the SignUp component where I dispatch.
import React, { Component } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { createUser } from '~/redux/modules/authentication';
import './SignUp.css';
class SignUp extends Component {
static propTypes = {
dispatch: PropTypes.func.isRequired
}
constructor(props) {
super(props);
this.state = {
username: '',
displayName: '',
email: '',
password: '',
confirmPasswordText: ''
}
}
handleUsernameChange = (event) => {
this.setState({username: event.target.value});
}
handleDisplayNameChange = (event) => {
this.setState({displayName: event.target.value});
}
handleEmailChange = (event) => {
this.setState({email: event.target.value});
}
handlePasswordChange = (event) => {
this.setState({password: event.target.value});
}
handleConfirmPasswordChange = (event) => {
this.setState({confirmPasswordText: event.target.value});
}
stopDefault = (event) => {
event.preventDefault();
createUser(this.state);
}
render () {
return (
<div className="sign-up">
<div>
<h1>Nimbus</h1>
</div>
<form onSubmit={this.props.dispatch(this.stopDefault())}>
<div className="form-group">
<label>Username</label>
<input
type="text"
className="sign-up__input form-control"
id="formGroupExampleInput"
placeholder="Choose a username"
onChange={this.handleUsernameChange}
/>
</div>
<div className="form-group">
<label>Display Name</label>
<input
type="text"
className="sign-up__input form-control"
id="formGroupExampleInput2"
placeholder="Choose a display name"
onChange={this.handleDisplayNameChange}
/>
</div>
<div className="form-group">
<label>Email</label>
<input
type="email"
className="sign-up__input form-control"
id="formGroupExampleInput3"
placeholder="Enter your email address"
onChange={this.handleEmailChange}
/>
</div>
<div className="form-group">
<label>Password</label>
<input
type="password"
className="sign-up__input form-control"
id="formGroupExampleInput4"
placeholder="Password"
onChange={this.handlePasswordChange}
/>
</div>
<div className="form-group">
<label>Confirm password</label>
<input
type="password"
className="sign-up__input form-control"
id="formGroupExampleInput4"
placeholder="Password"
onChange={this.handleConfirmPasswordChange}
/>
</div>
<button type="submit" className="sign-up__button btn btn-
primary btn-lg">Sign Up</button>
</form>
</div>
);
}
}
export default connect()(SignUp);
~doing in your import calls? that's not a standard thing, and it will be difficult to reproduce your issue without knowing how to support that. - rossipediacreateUsermethod? You usemapDispatchToPropsorbindActionCreators? It would be helpful if you can share that part of your code as well. - Tharaka Wijebandaradispatch(createUser), which won't work - it needs to bedispatch(createUser())(see stackoverflow.com/questions/44334008/… for the same issue from someone else earlier today). - markeriksoneventis undefined in thestopDefaultfunction?? - maxwellgoverstopDefaultin<form onSubmit={this.props.dispatch(this.stopDefault())}>- Anthony Kong