I am receiving an error "'$' is not defined" in my React web app. I use create-react-app to initialize the app. I read that I need to import jquery $ into my app but I did not need to use that for another react application I made.
The problem comes from my lines within the _login() function:
const email = $("#email").val()
const password = $("#password").val()
I am getting the values of email and password from the input tags using template literals. From using React 16.0 I should be fine not import $ from jQuery, especially if my other project did not have to.
import React, {Component} from 'react';
import styled from 'styled-components';
import axios from 'axios';
const Container = styled.div`
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
`;
class LoginScreen extends Component {
constructor(props){
super(props);
this.state ={
clients: [],
projects: [],
}
}
_login = () => {
const email = $("#email").val()
const password = $("#password").val()
const request = {"auth": {"email": email, "password": password}}
console.log(request)
axios.post('http://localhost:3000/api/user_token',
'auth': request
).then(response => {
console.log(response)
}).catch(error => console.log(error))
}
render(){
return(
<Container>
Login
<form>
<label>Email: </label>
<input
id="email"
name="email"
type="email"
/>
<label>Password: </label>
<input
id="password"
name="password"
type="password"
/>
</form>
<button
onClick={this._login}
>
Log In
</button>
</Container>
)
}
}
export default LoginScreen;
Package.json
"dependencies": {
"@material-ui/core": "^1.1.0",
"axios": "^0.18.0",
"immutability-helper": "^2.7.0",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-redux": "^5.0.7",
"react-router-dom": "^4.2.2",
"react-scripts": "1.1.4",
"styled-components": "^3.3.0"
},