I am trying to redirect signup function to home page in react, using usehistory Hook when i run my code it display such errore ::
JavaScript
x
2
1
export 'useHistory' (imported as 'useHistory') was not found in 'react-router-dom' (possible exports: BrowserRouter, HashRouter, Link, MemoryRouter, NavLink, Navigate, Outlet, Route, Router, Routes, UNSAFE_LocationContext, UNSAFE_NavigationContext, UNSAFE_RouteContext, createRoutesFromChildren, createSearchParams, generatePath, matchPath, matchRoutes, renderMatches, resolvePath, unstable_HistoryRouter, useHref, useInRouterContext, useLinkClickHandler, useLocation, useMatch, useNavigate, useNavigationType, useOutlet, useOutletContext, useParams, useResolvedPath, useRoutes, useSearchParams)
2
versions:”react”: “^17.0.2”, “react-router-dom”: “^6.2.1”
below is my login code given:
JavaScript
1
68
68
1
import {useState,React} from 'react'
2
import "./login.css"
3
import {
4
Link,useHistory
5
} from "react-router-dom";
6
7
8
import {auth,db} from "./firebase"
9
function Login() {
10
//taking input value from email and password field
11
const history =useHistory()
12
const[email,setEmail]=useState("")
13
const [password,setPassword]=useState("")
14
15
const signIn= e =>{
16
e.preventDefault()
17
console.log("it works")
18
19
}
20
21
22
23
const signUp = e =>{
24
e.preventDefault()
25
26
auth.createUserWithEmailAndPassword(email,password)
27
.then((auth)=> {
28
console.log(auth)
29
if(auth){
30
history.push("/")
31
}
32
})
33
.catch(error =>alert(error.message))
34
}
35
36
37
38
39
return (
40
<div className="login">
41
<Link to ="/">
42
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/Amazon_logo.svg/2560px-Amazon_logo.svg.png" alt="amazon logo" className="login__image" />
43
</Link>
44
<div className="login__container">
45
<h2 className="signLabel">sign-in</h2>
46
<form>
47
<h5 className="label">E-mail</h5>
48
<input type="text" onChange={e=>setEmail(e.target.value)} value={email} placeholder="Enter your Email"/>
49
<h5 className="label">password</h5>
50
<input type="Password" onChange={e=>setPassword(e.target.value)} value={password} placeholder="Enter your password" />
51
52
<button type ="submit" onClick={signIn}className="login__sign__in">sign in</button>
53
</form>
54
55
<p className="privacy">
56
before sign in please read the notice. we are not responsible for your data
57
</p>
58
<button type="submit" onClick={signUp} className="login__create__account">
59
create new account
60
</button>
61
</div>
62
63
</div>
64
)
65
}
66
67
export default Login
68
Advertisement
Answer
useHistory is not available in v6, useNavigate Instead of useHistory,
Check here https://reactrouter.com/docs/en/v6/upgrading/v5#use-usenavigate-instead-of-usehistory
JavaScript
1
12
12
1
// v6
2
3
import { useNavigate } from 'react-router-dom';
4
5
function MyButton() {
6
let navigate = useNavigate();
7
function handleClick() {
8
navigate('/home');
9
};
10
return <button onClick={handleClick}>Submit</button>;
11
};
12