I am new to ReactJS and creating a login page using React JS and laravel API. When I click the login button after inserting the email address and password it says the above error.
This is my code
import React, { Component } from 'react';
import { Button, Card, CardBody, CardGroup, Col, Container, Input, InputGroup, InputGroupAddon, InputGroupText, Row } from 'reactstrap';
import axios from 'axios';
import { Redirect } from 'react-router-dom'
class Login extends Component
{
onClick(event)
{
var self = this;
var payload={
"email":this.state.email,
"password":this.state.password
}
//"role":this.state.loginRole
axios.post('http://127.0.0.1:8000/api/login', payload)
.then(function (response)
{
console.log(response);
if(response.data.code == 200)
{
//Set localstorage:
const userDetails = {userName: this.state.email}
localStorage.setItem('userDetails', JSON.stringify(userDetails));
console.log("Login successfull");
return <Redirect to='/Master' />
var uploadScreen=[];
// uploadScreen.push(<UploadPage appContext={self.props.appContext} role={self.state.loginRole}/>)
self.props.appContext.setState({loginPage:[],uploadScreen:uploadScreen})
}
else if(response.data.code == 204)
{
console.log("Username password do not match");
alert(response.data.success)
}
else
{
console.log("Username does not exists");
alert("Username does not exist");
}
})
.catch(function (error)
{
console.log(error);
});
}
render()
{
return (
<div className="app flex-row align-items-center">
<Container>
<Row className="justify-content-center">
<Col md="8">
<CardGroup>
<Card className="p-4">
<CardBody>
<h1>Login</h1>
<p className="text-muted">Sign In to your account</p>
<InputGroup className="mb-3">
<InputGroupAddon addonType="prepend">
<InputGroupText>
<i className="icon-user" />
</InputGroupText>
</InputGroupAddon>
<Input type="text" placeholder="Username" />
</InputGroup>
<InputGroup className="mb-4">
<InputGroupAddon addonType="prepend">
<InputGroupText>
<i className="icon-lock" />
</InputGroupText>
</InputGroupAddon>
<Input type="password" placeholder="Password" />
</InputGroup>
<Row>
<Col xs="6">
<Button color="primary" className="px-4" onClick={(event) => this.handleClick(event)}>
Login
</Button>
</Col>
<Col xs="6" className="text-right">
<Button color="link" className="px-0">
Forgot password?
</Button>
</Col>
</Row>
</CardBody>
</Card>
<Card
className="text-white bg-primary py-5 d-md-down-none"
style={{ width: 44 + "%" }}
>
<CardBody className="text-center">
<div>
<h2>Sign up</h2>
<p>
Are you a new user to the Restaurant system? Hooray now , you can create an account...
</p>
<Button color="primary" className="mt-3" active>
Register Now!
</Button>
</div>
</CardBody>
</Card>
</CardGroup>
</Col>
</Row>
</Container>
</div>
);
}
}
export default Login;
In this case I gave the following as the payload
Username : admin@admin.com
Password : 123456
the above credentials are valid in Laravel API. What wrong in this code to get error as TypeError: _this2.handleClick is not a function
?