0

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.

user18309290
  • 5,777
  • 2
  • 4
  • 22
  • 1
    Ensure that you are using a version of React Router that supports the state property in the to prop of the Link component. This feature is available in React Router version 5 and above. – Mile Mijatović Aug 25 '23 at 11:49

0 Answers0