1

I am trying with some components change before and after the certain page load.

already some questions are asked about it but I understand none of them.

I tried using it like this

<Route exact path="/404">
      <PageNotFound
        onEnter={(nexState, replace, cb) => {
          console.log(nexState);
          console.log(replace);
          console.log(cb);
        }}
      />
    </Route>

I also tried the onEnter function adding to the Route itself but both of the time no console . what I'm trying to do. I have a sad face SVG on the 404 PageNotFound pages with the text "to be the happy go to home page", and if the user leaves a 404 page from any menu link I want to turn sad face to happy face SVG before the page change. and again sad SVG if the user encounters 404 pages again

import React, { useState, useRef, useEffect } from "react";

import { Link } from "react-router-dom";
import { useSpring, animated } from "react-spring";

const calc = (x, y) => [x - window.innerWidth / 2, y - window.innerHeight / 2];
const trans1 = (x, y) => `translate3d(${x / 10}px,${y / 10}px,0)`;
const trans2 = (x, y) => `translate3d(${x / 8}px,${y / 8}px,0)`;
const trans4 = (x, y) => `translate3d(${x / 5}px,${y / 5}px,0)`;

const PageNotFound = () => {
  const [happy, setHappy] = useState(false);

  const svg = useRef(null);
  useEffect(() => {
    svg.current.classList.remove("active");
  }, [happy]);
  const [props, set] = useSpring(() => ({
    xy: [0, 0],
    config: { mass: 10, tension: 550, friction: 140 },
  }));
  const happyMode = (e) => {
    svg.current.classList.add("active");
  };

  return (
    <>
      <main
        className="page-404"
        onMouseMove={({ clientX: x, clientY: y }) => set({ xy: calc(x, y) })}
      >
        <section className="screen">
          <div className="section__rule vh">
            <article>
              <h1 className="section__title">OOPS !</h1>
              <p>
                Looks like you are sad.
                <br /> Go
                <Link
                  to="/"
                  onClick={(e) => {
                    happyMode(e);
                  }}
                >
                  <b>back home</b>
                </Link>
                make a smile.
              </p>
              <div className="wrapper--404">
                <svg
                  width="474"
                  height="257"
                  viewBox="0 0 474 257"
                  fill="none"
                  xmlns="http://www.w3.org/2000/svg"
                  ref={svg}
                  className="active"
                >
                  <g>
                    <animated.g
                      className="logoItem"
                      style={{ transform: props.xy.interpolate(trans1) }}
                    >
                      <path
                        d="M153.095 256.625V201.763C177.584 188.542 205.58 181.045 235.348 181.045C265.286 181.045 293.453 188.61 318.043 202.002V257C295.735 238.054 266.852 226.604 235.348 226.604C204.048 226.604 175.37 237.883 153.095 256.625Z"
                        fill="#00AAE7"
                      />
                      <path
                        d="M235.348 164.654C280.749 164.654 317.567 127.818 317.567 82.3954C317.601 36.9041 280.783 0.0681152 235.348 0.0681152C189.948 0.0681152 153.13 36.9041 153.13 82.3613C153.13 127.818 189.948 164.654 235.348 164.654ZM235.348 45.6275C255.614 45.6275 272.064 62.1202 272.064 82.3613C272.064 102.602 255.614 119.095 235.348 119.095C215.083 119.095 198.633 102.602 198.633 82.3613C198.633 62.1202 215.117 45.6275 235.348 45.6275Z"
                        fill="#03256C"
                      />
                    </animated.g>
                    <animated.path
                      d="M76.6329 134.634H0V103.148L72.069 0H115.631V100.865H134.329V134.634H115.631V164.314H76.6329V134.634ZM79.3576 100.865V42.9015L41.0412 100.865H79.3576Z"
                      fill="#03256C"
                      style={{ transform: props.xy.interpolate(trans2) }}
                    />
                    <animated.path
                      style={{ transform: props.xy.interpolate(trans4) }}
                      d="M416.304 134.634H339.671V103.148L411.74 0H455.302V100.865H474V134.634H455.302V164.314H416.304V134.634ZM419.029 100.865V42.9015L380.713 100.865H419.029Z"
                      fill="#03256C"
                    />
                  </g>
                  <defs>
                    <clipPath id="clip0">
                      <rect width="474" height="257" fill="white" />
                    </clipPath>
                  </defs>
                </svg>
              </div>
            </article>
          </div>
        </section>
      </main>
     
    </>
  );
};

export default PageNotFound;

404page it is

anjit pariyar
  • 51
  • 2
  • 7
  • `onEnter` and `onLeave` are dom element events involving the mouse, I don't think they do what you want here when entering or leaving a specific path. You can create a custom history object and listen for route transitions though. – Drew Reese Mar 14 '21 at 18:40
  • they are doing prompt for the filed form. https://reactrouter.com/web/api/Prompt . and some of them are doing this too https://stackoverflow.com/questions/32841757/detecting-user-leaving-page-with-react-router .. any idea how to detect page leave?? – anjit pariyar Mar 14 '21 at 18:45
  • 1
    What is "they"? "Some of them are doing this too", what is "them"? Sounds like you want a check for something when a route's component is mounted, and another when it unmounts. Can you provide in your question a [Minimal, Complete, and Reproducible](https://stackoverflow.com/help/minimal-reproducible-example) code example of what you are trying to do? – Drew Reese Mar 14 '21 at 18:50
  • `PageNotFound` is some component that receives an `onEnter` prop. What does it *do* with that prop? When does it call it? Just setting a prop `onEnter` doesn't do anything. It is up to you as the code author to *use* the prop in your component. – Linda Paiste Mar 14 '21 at 19:04
  • Sorry for the confusion. let me tell you that I only use functional components and explain what I'm trying to do. I have a sad face SVG on the 404 **PageNotFound** pages with the text "to be the happy go to home page", and if the user leaves a 404 page from any menu link I want to turn sad face to happy face SVG before the page change. and again sad SVG if the user encounters 404 pages again. I do not feel code is necessary for it – anjit pariyar Mar 14 '21 at 19:07
  • "I do not feel code is necessary for it " You don't feel code is necessary to accomplish what you want? Or you don't feel it necessary to share code with us for what you are wanting to do so someone can help? You could detect the page unmount (*or swap to happy face when a link is clicked*), but to be completely honest, since it's unmounting because the router is transitioning to another route, there isn't really any point in trying to update your `PageNotFound` UI, the user likely won't see it. – Drew Reese Mar 14 '21 at 19:16
  • Drew Reese, I edited the question with code. Please Help me – anjit pariyar Mar 14 '21 at 19:40

0 Answers0