I was trying to route the page if my http response is successful to the new page i.e. landing page.
I have written code for handling http request and its working fine and returning response, if my response is successfull or the login is successful i.e. true then i want to move to my next page i.e my component <Loading />
with some parameter if my response fails then it should be on the samepage
Basically i was trying when i click on login button it should send a http request if the request return a response then it should switch over to another page else it should be on the same page
const Login = () => {
const [userName , setUserName] = useState("")
const [userPassword , setUserPassword] = useState("")
const handleUsernameInput = (event) => {
setUserName(event.target.value);
console.log("username entered")
}
const handlePasswordInput = (event) => {
setUserPassword(event.target.value);
console.log("password entered")
}
const [httpData, apiMethod] = useHttpPOSTHandlerdotthen()
const handleSubmit = () => {
apiMethod({url: 'url' , data: { "password": userPassword, "username": userName }})
setUserName("")
setUserPassword("")
nextPage();
}
const nextPage = () => {
if (httpData) {
return <Redirect to={{ pathname: '/landing', key: httpData.key}} />
}
else{
return <Redirect to={{ pathname: '/' }} />
}
}
return (
<div className = "login-page">
<Form>
<Form.Control size = "sm"
type="text"
placeholder="Username"
value = {userName}
onChange = {event => handleUsernameInput(event)}
/>
<Form.Control size = "sm"
type="password"
placeholder="Password"
value = {userPassword}
onChange = {event => handlePasswordInput(event)}
/>
<Button size = "sm" variant="success"
onClick = {handleSubmit}>Login</Button>
</Form>
</div>
);
};
export default Login;