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

199
Views
How can I detect browser back button in react class component

I am trying to create a function when user hit browser back button it will run function deleteHeldResort that I created.

Here is my code for deleteHeldResort:

deleteHeldResorts(ReservedInventoryID: number | null = null, refreshHeldResorts: boolean = true) {
    return new Promise((resolve, reject) => {
        this.setState({heldResortsShowLoader: true});
        this.reservationService.deleteHeldResorts(ReservedInventoryID)
            .then(() => {
            refreshHeldResorts && this.getHeldResorts();
            resolve();
        })
        .catch((error: any) => {
            this.catchHeldResortsError(error);
            reject(error);
        });
        this.setState({
            heldResortsShowLoader: false
        });
    });
}

Updated code base:

  handleNavigateBack = useCallback(
    (event) => {
             
           // call your function here with whatever argument your code provides
           this.reservationService.deleteHeldResorts(this.props.resId);

      }
    , []);
  
  useEffect(() => {
    document.addEventListener('popstate', this.handleNavigateBack );
    return () => {
      document.removeEventListener('popstate', this.handleNavigateBack)
    }
}, [this.handleNavigateBack]);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Updated answer: class component based

Since you're using class components, I'm adding this update. Inside the class component having deleteHeldResorts function, you can listen to popstate event: Remark how bind keyword helps us keep the right this.


import React from "react";
import { render } from "react-dom";

class App extends React.Component {
  constructor(props) {
    super(props);
    this.handleNavigateBack = this.handleNavigateBack.bind(this);
  }

  componentDidMount() {
    document.addEventListener("popstate", this.handleNavigateBack);
  }

  componentWillUnmount() {
    document.removeEventListener("popstate", this.handleNavigateBack);
  }

  handleNavigateBack(event) {
    console.log("inside callback", event);
   // change arguments as you want
    this.deleteHeldResorts(null, false);
  }

  deleteHeldResorts(ReservedInventoryID = null, refreshHeldResorts = true) {
    // your function goes down there content
    console.log("inside deleteHeldResorts");
  }

  render() {
    return <h2>popstate browser listener</h2>;
  }
}

render(<App />, document.getElementById("root"));


Initial answer: function component based

Inside the component having deleteHeldResorts function, you can listen to popstate event:


// your-component.js

function YourComponent() {

   function deleteHeldResorts(ReservedInventoryID: number | null = null, 
                              refreshHeldResorts: boolean = true) {
               // your function content
      }
 
  const handleNavigateBack = useCallback(
    (event) => {
             
           // call your function here with whatever argument your code provides
           deleteHeldResorts(reservedInventoryID,refreshHeldResorts);

      }
  //  depending on your logic, add deps to this array dependency 
    }, [])


  useEffect(() => {
    document.addEventListener('popstate', handleNavigateBack );
    return () => {
      document.removeEventListener('popstate', handleNavigateBack)
    }
  }, [handleNavigateBack])

 return ( <>something dope</>);
}
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!