0

I'm stuck. I can't redirect my user after my request .

There is my request (in component):

const onSubmit = (data) =>{ 
    const datafull = 
      {
      "title": data.title,
      "excerpt": "nonex5",
      "content": data.content,
      "location": data.location,
      "company_id": "75",
      "category_id": data.category_id,
      "date_start_at": data.date_start_at,
      "date_end_at": data.date_end_at,
      "link" : data.link,
      }
    console.log(datafull);
    axios({
      method: 'post',
      url: 'url',
      data: datafull,
    })
    .then(function (res) {
      {
       console.log('resolution',res.data);
       return <Redirect to="/company/news" />
      };
    })
    .catch(function (erreur) {
        console.log(erreur);
    });};
  const { register, handleSubmit, errors } = useForm();
  

Do anyone have suggestion to help me to get to the solution?

Sebastian B.
  • 2,141
  • 13
  • 21

4 Answers4

0

you can use the history that is in the route props.

import { useHistory } from "react-router-dom";

const history = useHistory();
history.replace("/company/news"); or history.push("/company/news");

I hope you find it useful

Daymel
  • 3
  • 1
0

You can use the history hook to go from one route to another

import { useHistory } from 'react-router-dom';

const YourComponent = () => {
    const history = useHistory(); 

    // trigger this method to go to news page
    function goToNews() {
        history.push('/company/news');
    }


}

Or you can use window object also -

window.location.replace("url") 
//or 
window.location.assign("url")
Dlucidone
  • 1,091
  • 9
  • 23
0

Ty !!! problem solved with :

axios({
      method: 'post',
      url: 'url',
      data: datafull,
    })
    .then(function (res) {
      {
        console.log('resolution',res.data);
        return history.push("/company/news");
      };
    })
    .catch(function (erreur) {
        
        console.log(erreur);
        
    });};

Thanks Thanks !

0

Try out this solution:

import React, { useState } from "react";
import axios from "axios";
import { useForm } from "react-hook-form";
import { Redirect } from "react-router-dom";

function ResponseHandler() {
  const { register, handleSubmit, errors } = useForm();

  const [redirect, setRedirect] = useState(false);

  const onSubmit = (data) => {
    const datafull = {
      title: data.title,
      excerpt: "nonex5",
      content: data.content,
      location: data.location,
      company_id: "75",
      category_id: data.category_id,
      date_start_at: data.date_start_at,
      date_end_at: data.date_end_at,
      link: data.link,
    };

    console.log(datafull);
    axios({
      method: "post",
      url: "url",
      data: datafull,
    })
      .then(function (res) {
        console.log("resolution", res.data);
        setRedirect(true);
      })
      .catch(function (err) {
        console.log(err);
      });
  };

  if (redirect) return <Redirect to="/company/news" />;
  // default return goes beneath
}

Let me know if it helps.

Jawad ul hassan
  • 565
  • 1
  • 4
  • 19