Link component in react problems whit passing arguments

33 Views Asked by At

I'm working on a project based on react. I'm trying to pass some stuff through the to of the Link component but this does not work. The previous page return me always "/home" and data return me "undefined". Someone can help me with this code? Maybe i wrote something in a bad way.

Here the Link code

                       <Link
                         to={{
                           pathname: "/home/newtask",
                           state: {
                             previousPage: window.location.pathname,
                             data: "event",
                           },
                         }}
                         className="btn"
                         style={{
                           textDecoration: "inherit",
                           backgroundColor: "#c5fdc8",
                           width: "100px",
                           cursor: isButtonDisabled
                             ? "not-allowed"
                             : "pointer",
                         }}
                       >
                         New Event
                       </Link>

Here how i recall it form the newtask page

  const location = useLocation();
  const previousPage = location.state?.previousPage || "/home"; 
  console.log(previousPage);
  console.log(location.state?.data);

I will appreciate very much an help on this.

1

There are 1 best solutions below

0
Alessia Volpi On

I solved the problem by changin the approach. i use the navigate in this way to pass other informations through the next page

navigate("/home/tasks/edittask", {
          state: {
            task: task,
            previousPage: window.location.href,
          }

they are not returned as a props but is possible to retrieve the information by using location.state