1

I am currently working with React and TypeScript. Here is my problem: When I try to do a history.push, it returns an error like this: Uncaught (in promise) TypeError: history.push is not a function

Here is my code:

interface Props extends 
WithTranslation {
  history: any;
}

const Menu: React.FC<Props> = ({
  history
}) => {
  if (await MarketplaceRequests.sessionsTest()) {
      history.push({
        pathname: '/',
      });
  }

return ......

Thank you in advance

1

2 Answers 2

3

I think in react-router-dom v6 useHistory doesn't work or its deprecated. Instead use useNavigate:

import {useNavigate} from 'react-router-dom'

const navigate = useNavigate()

const Menu: React.FC<Props> = ({
  navigate
}) => {
  if (await MarketplaceRequests.sessionsTest()) {
      navigate('/');
  }

Sign up to request clarification or add additional context in comments.

Comments

1

If you're using React Router Dom > v6, you need to use useNavigate.

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

const navigate = useNavigate();

navigate('/route');

However, if not, you need to import useHistory as same as useNavigate in the example above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.