8

I want to upgrade the react-router package to v6. It has a dependency on the history package, and in the history package roadmap, the document says history.length will be deprecated and even deleted soon. You can take a look https://github.com/remix-run/history/issues/689

I wonder if there are other alternatives for using history.length in my code while upgrading the react-router package to the latest version.

As an example, one of my usage of it:

  import { useHistory } from 'react-router';

  const history = useHistory();

  const onGoBack = () => {
    if (history.length > 2) {
      history.goBack();
    } else {
      history.push('/portfolio');
    }
  };
Emre Doğan
  • 137
  • 2
  • 11
  • 1
    Well, in RRDv6 the `history` object is abstracted away anyway and not exposed out. You'll need some custom components to continue using a custom `history` object. That said, you may need to instead just maintain your own "history stack" in app state. I implemented something similar in Redux so I could peek at the "previous" location in the app. – Drew Reese Feb 04 '22 at 16:46
  • Thanks, @DrewReese, for your helpful answer. I will implement an approach like storing this data in the context. – Emre Doğan Feb 11 '22 at 09:33

0 Answers0