I am new to React and am having difficulty.
In my Login Component I have the user click login which points to the handleLogin function
const handleLogin = () => {
(userService.login(username, password);
setUsername('');
setPassword('');
};
this then calls the login functon within the userService:
class UserService {
login(username, password) {
Axios.post('http://localhost:3001/login', {
username: username,
password: password,
}).then((response) => {
if (response.data.message) {
return response.data.message;
} else {
localStorage.setItem('token', response.data.token);
localStorage.setItem('user', response.data.result[0].id);
return response.data.result[0];
}
});
}
}
export default UserService;
After calling this function I want it to update the state which will rerender the form and change my login button to a logout button. However, every time I run it it is resetting the state first and then performing the userService.login function. How can I correct this?