Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
Observer back button click from a certain page (Reactjs)

Is their any way we can observer back button click in react ?

Actually, i have added a code on category page, the functionality it provides is to remain the user anchored on page.

For example : customer browse category page scroll through products & click product page then click back button then user see exactly the same category page products which user clicked (i.e same product position).

For this i set below code on category page

CategoryPage.js

const [scrollPosition, setScrollPosition] = React.useState('');

 React.useEffect(() => {
   var pathName = document.location.pathname.substr(1);
   if (window) {
     window.onscroll = function (e) {
       setScrollPosition(window.scrollY);
       if(scrollPosition != 0){
         sessionStorage.setItem("scrollPosition_"+pathName, scrollPosition);
       }
     };
   }
 }, [scrollPosition]);

 React.useEffect(() => {
   var pathName = document.location.pathname.substr(1);
   if (window && sessionStorage.getItem("scrollPosition_"+pathName)) {
     $(window).scrollTop(sessionStorage.getItem('scrollPosition_'+ pathName));
     console.log('position_set to = ' + sessionStorage.getItem('scrollPosition_'+ pathName));
   }
 }, []);

As you can see code scrollTop the screen with value saved in session storage It works but i need this thing to work only when this scenario (i.e user visit category page, scroll through page, click product, product page opens & click back button)

It should not work with any other scenario like (user visit category page, scroll through page, click product, product page opens, user click another page or homepage & visit Same category page & with above code it scrolls it to the same page's position).

Solution : Check the previous route is product page then scroll top else do nothing

is their any fix for this ? how to check the previous route is product page ?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

We can observe a back button click, by listening to the history object. Try the below example,

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

const Test = () => {

  const history = useHistory();
  
  useEffect(() => {
    return () => {
      if (history.action === "POP") {
        
      }
    };
  }, [history])
}

event POP would mean, back button click.

Ref:- https://reactrouter.com/web/api/history

You could get to know the previous page through location history

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

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

  useEffect(() => {
    return history.listen(location => {
      if (history.action === 'POP') {
        
      }
    })
  }, [])
}

location.from will give you the previous route.

Update in response to comments:

I see you're using next/router which has beforePopState for this, it takes a callback and if that cb returns false, then router will not do anything for the POP event.

Ref: https://nextjs.org/docs/api-reference/next/router#routerbeforepopstate

Keep the route history i.e, pathname or asPath in a global state and thereby you could find out if the previous route was product page and on back button click (beforePopState) you could execute your code.

about 4 years ago · Juan Pablo Isaza Report

0

You can't observe what a user is doing in the browser app itself (i.e. clicking the back button), but you can listen for "popstate" events. https://developer.mozilla.org/en-US/docs/Web/API/History_API#examples In React you can use history.

I think you'll find instantiating a history object and using it to listen will be easier for you.

history

import { createBrowserHistory } from 'history';

const history = createBrowserHistory();

const useBackListener = (callback) => {
  const listener = (location, action) => {
    if (action === "POP") {
      // back navigation
      callback(location, action);
    }
  };

  useEffect(() => {
    const unlisten = history.listen(listener);
    return unlisten;
  }, []);

  return path;
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!