Home > Blockchain >  × TypeError: history.push is not a function
× TypeError: history.push is not a function

Time:05-24

18 | const {error,loading,userInfo} = userLogin;
  19 | useEffect(() =>{
  20 |   if (userInfo) {
> 21 |     history.push(redirect);
     | ^  22 |   }
  23 | },[userInfo,history,redirect])
  24 | 

I have these Login.js File I want to login in my app but it is showing the error

CodePudding user response:

I assume you're trying to use react-router-dom's useHistory hook, and in that case you'll need to import it first

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

and then declare it before using it like so

const history = useHistory();

For more information on React-router and how to use it, see the github

CodePudding user response:

If you are using react-router-dom v6, they changed the way to redirect.

import {useNavigate} from 'react-router-dom' //this is the import in the head of the file

 | const {error,loading,userInfo} = userLogin;
  const navigate = useNavigate()
 
  19 | useEffect(() =>{
  20 |   if (userInfo) {
> 21 |     navigate(redirect);
     | ^  22 |   }
  23 | },[userInfo,history,redirect])
  24 | 

  • Related