I create a basic log page in react where on login it will redirect me to the next component I use react-router v5.3 it is showing an error cannot read properties of undefined (reading 'push') how can I resolve this error
import React, {useState} from 'react'
import {useHistory,} from 'react-router-dom';
export default function Loginpage() {
const history = useHistory();
//set the initial sate for the inputs
const [username, setusername] = useState('');
const [password, setpassword] = useState('');
//function to handle form submission
const handleFormSubmit = (event) => {
event.preventDefault();// prevent from refreshing the page
//checck the user name and password
if (username === 'employee' && password === 'emp123'){
history.push('./components/JobFindPage');
alert('hii');
}
else if (username === 'employer' && password === 'emr123'){
history.push('/CreateJobPage');
alert('hii');
}
else{
alert ('wrong username or password')
}
}
return (
<>
<form onSubmit = {handleFormSubmit}>
<label>
Username :
<input type="text" value={username} onChange={(e) => setusername(e.target.value)} /> <br />
</label>
<label>
Password :
<input type="text" value={password} onChange={(e) => setpassword(e.target.value)} /> <br />
</label>
<button type="submit">Log IN</button>
</form>
</>
);
}
I expected that using history push(/component) will redirect me to that component