tony_h
tony_h

Reputation: 95

How can I reroute to a new page if a POST operation is successful?

After I fill a form in react, and I posted to the working API, I want it to reroute back to /dashboard. I will paste the code that is working

import axios from 'axios';
import { GET_ERRORS } from './types';


const createProject = (project, history) => async dispatch => {
  await axios.post('http://localhost:8080/api/project', project)


    .catch((error) => {
      dispatch({
        type: GET_ERRORS,
        payload: error.response.data
      });
    });
};


export default createProject;

Upvotes: 0

Views: 46

Answers (1)

mruanova
mruanova

Reputation: 7105

use History instead of window.location

// src/actions/userActionCreators.js

// ...
import history from '../history';
import axios from 'axios';
import { GET_ERRORS } from './types';

const createProject = (project, history) => async dispatch => {
  await axios.post('http://localhost:8080/api/project', project)

    history.push('/');

    .catch((error) => {
      dispatch({
        type: GET_ERRORS,
        payload: error.response.data
      });
    });
};

export default createProject;

Upvotes: 1

Related Questions