I'm trying to get the login function working, I've tried to learn from the examples, but keep gets the error TypeError: Cannot read property 'state' of undefined.
I'm not sure which part is causing the problem... The example had the handlesubmit as well but I thought the login function would just do the similar job.
All I'm trying to do is to let the inputs read the value that I type in and use it... please help
import React, { Component } from 'react';
import { Link } from 'react-router-dom'
import '../App.css';
import ReactDOM from 'react-dom';
import jwt from 'jsonwebtoken'
const API_URL = "http://131.181.190.87:3000"
function login(){
const url = `${API_URL}/user/login`
const inputemail = {email: this.state.email};
const inputpassword = {password: this.state.password};
return fetch(url,{
method: "POST",
headers: { accept:"application/json","Content-Type":"application/json"},
body: JSON.stringify({inputemail,inputpassword})
})
.then((res) =>res.json())
.then((res) => {
console.log(res)
})
}
class LoginPage extends Component {
constructor(props){
super(props);
this.state={
email:'',
password:''
};
this.handleChange = this.handleChange.bind(this);
}
handleChange(event){
this.setState({
[event.target.name]: event.target.value
});
}
render(){
return (
<div className = "Login_Page">
<h2>Login with your email and password</h2>
<form className = 'Spacing'>
<label className = 'Spacing_left'>
Email:
</label>
<input
type="email"
name="email"
placeholder="Email"
value={this.state.email}
onChange={this.handleChange}
required />
</form>
<form className = 'Spacing'>
<label>Password:</label>
<input
type="password"
name="password"
placeholder="Password"
value={this.state.password}
onChange={this.handleChange}
required />
</form>
<br/>
<button onClick={login}>Login</button>
<h2>Not Registered yet? Register now!</h2>
</div>);
}
}
export default LoginPage;