Dream_Cap
Dream_Cap

Reputation: 2302

Why won't Axios give me a response with Redux actions?

I am trying to make a simple signup form. I have a redux form that I'm trying to send some user data to my express backend with. I am trying to do this through redux actions via this redux action:

Ultimately, I'd like to receive a response, and redirect or give errors if necessary. The backend seems to receive the data and can validate, but axios receiving the response to let redux know to update the state. Any ideas?

Edit: I also tried putting the axios request inside of the signupForm itself, and still had issues(wasn't able to get a response).

Edit: Here is the repo if you'd like to see all the files: https://github.com/capozzic1/react-blog

redux signup action:

import axios from 'axios';
import { SubmissionError } from 'redux-form';


/* eslint-disable */
export const signUp = userData => dispatch => axios.post('/api/users', userData)
.then((response) => {
  dispatch({ type: 'SIGNUP_REDIRECT_YES ', payload: true})
  // this.props.history.go('/');
  console.log(response);
})
.catch((error) => {
  console.log(error.response);
  dispatch({ type: 'SIGNUP_REDIRECT_NO ', payload: false})
  throw new SubmissionError({ _error: 'Login failed!' });
});

Also with this signup form component (redux-form):

class SignUpForm extends React.Component {
  constructor(props) {
    super(props);

    this.onSubmit = this.onSubmit.bind(this);
  }

  onSubmit(userData) {
    this.props.signup(userData);

  }

  render() {
    const { error, handleSubmit, pristine, reset, submitting } = this.props;
    return (
      <form >
        {error && (<strong>{error}</strong>)}
        <Field name="username" type="text" component={renderField} label="Username" />
        <Field name="email" type="email" component={renderField} label="Email" />
        <Field name="password" type="text" component={renderField} label="Password" />
        <Field name="passwordConfirm" type="text" component={renderField} label="Enter password to confirm" />

        <div>
          <button type="button" disabled={submitting} onClick={handleSubmit(this.onSubmit)}>Sign Up</button>
          <button type="button" onClick={reset}>Clear Values</button>
        </div>
      </form>
    );
  }
}


export default reduxForm({
  form: 'signUpForm',


})(SignUpForm);

This form is being fed by a container component(thought this was a standard pattern? Let me know if it's not). Sign up page container:

const mapDispatchToProps = dispatch => ({
  signup: (user) => {
    dispatch(signUp(user));
  },
});
const SignUp = props => (
  <Layout>
    <SignUpForm signup={props.signup} />;
  </Layout>
);

export default connect(null, mapDispatchToProps)(SignUp);

Here is the sign up reducer:

export default function reducer(state = {
  signupRedirect: false,
}, action) {
  switch (action.type) {
    case 'SIGNUP_REDIRECT_YES': {
      return {
        ...state, signupRedirect: action.payload,
      };
    }
    case 'SIGNUP_REDIRECT_NO' : {
      return {
        ...state, signupRedirect: action.payload,
      };
    }
  }

  return state;
}

Here is my store:

import { applyMiddleware, createStore, compose } from 'redux';
import { createLogger } from 'redux-logger';
import thunk from 'redux-thunk';
import promise from 'redux-promise-middleware';
import reducer from './reducers';


const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(reducer, composeEnhancers(
  applyMiddleware(promise(), thunk, createLogger()),
));

export default store;

Upvotes: 0

Views: 721

Answers (1)

Michał Pietraszko
Michał Pietraszko

Reputation: 6199

It doesn't work because your anonymous function returned by signUp function returns a promise. Use brackets to avoid default behaviour.

export const signUp = userData => dispatch => {
  axios.post('/api/users', userData)
    .then((response) => {
      dispatch({ type: 'SIGNUP_REDIRECT_YES ', payload: true})
      // this.props.history.go('/');
      console.log(response);
    })
    .catch((error) => {
      console.log(error.response);
      dispatch({ type: 'SIGNUP_REDIRECT_NO ', payload: false})
      throw new SubmissionError({ _error: 'Login failed!' });
    });
  }

Upvotes: 1

Related Questions