0

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;
Vandesh
  • 6,368
  • 1
  • 26
  • 38
tony_h
  • 95
  • 2
  • 10

1 Answers1

1

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;
mruanova
  • 6,351
  • 6
  • 37
  • 55
  • possible duplicate https://stackoverflow.com/questions/42701129/how-to-push-to-history-in-react-router-v4 – mruanova Oct 16 '19 at 21:22