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

145
Views
React Router goBack does not work on buttonclick

I want to be able to go back to the previous page (just like using the back button in the browser), so I tried using useHistory's goBack(), but it does not work.

I imported useHistory:

import {
  BrowserRouter,
  Route,
  useHistory
} from "react-router-dom";

I wrote this function:

function GoBack(){
  const history = useHistory();

  return (
    <>
      <button onClick={() => history.goBack()}>Back</button>
    </>
);
}

I get this error:

TypeError: Cannot read properties of undefined (reading 'goBack')

This calls GoBack:

  function App() {
  const [user] = useAuthState(auth);
  return (
    <div className="App">
      <header className="App-header">
        <GoBack />
      </header>
      <section>{user ? <ChatRoom /> : <SignIn />}</section>
    </div>
  );
}

How can I fix this problem?

about 4 years ago · Juan Pablo Isaza
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!