Unexpected use of 'event' on Login - reactjs

I'm running a react-create-app and it's giving me the following errors on my below Login.js
./src/components/Login/Login.js
Line 6: 'handleClick' is not defined no-undef
Line 6: Unexpected use of 'event' no-restricted-globals
I've tried moving the handleClick event elsewhere in the js file but I continue to get the same error.
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {Button, Checkbox, Col, ControlLabel, Form, FormControl, FormGroup} from 'react-bootstrap';
import './Login.css';
handleClick(event)
{
const username = this.refs.username;
const password = this.refs.password;
const creds = { username: username.value.trim(), password: password.value.trim() };
this.props.onLoginClick(creds)
}
Login.propTypes = {
onLoginClick: PropTypes.func.isRequired,
errorMessage: PropTypes.string
};
class Login extends Component {
render() {
const { errorMessage } = this.props;
return (
<Form horizontal>
<FormGroup controlId="formHorizontalEmail">
<Col componentClass={ControlLabel} sm={2}>
Email
</Col>
<Col sm={10}>
<FormControl type="email" placeholder="Email" />
</Col>
</FormGroup>
<FormGroup controlId="formHorizontalPassword">
<Col componentClass={ControlLabel} sm={2}>
Password
</Col>
<Col sm={10}>
<FormControl type="password" placeholder="Password" />
</Col>
</FormGroup>
<FormGroup>
<Col smOffset={2} sm={10}>
<Checkbox>Remember me</Checkbox>
</Col>
</FormGroup>
<FormGroup>
<Col smOffset={2} sm={10}>
<Button onClick={(event) => this.handleClick(event)} type="submit">Sign in</Button>
</Col>
</FormGroup>
{errorMessage &&
<p style={{color:'red'}}>{errorMessage}</p>
}
</Form>
);
}
}
export default Login;

This ended up working:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {Button, Checkbox, Col, ControlLabel, Form, FormControl, FormGroup} from 'react-bootstrap';
import './Login.css';
class Login extends Component {
render() {
const { errorMessage } = this.props;
Login.propTypes = {
onLoginClick: PropTypes.func.isRequired,
errorMessage: PropTypes.string
}
function handleClick(event) {
const username = this.refs.username;
const password = this.refs.password;
const creds = { username: username.value.trim(), password: password.value.trim() };
this.props.onLoginClick(creds)
}
return (
<Form horizontal>
<FormGroup controlId="formHorizontalEmail">
<Col componentClass={ControlLabel} sm={2}>
Email
</Col>
<Col sm={10}>
<FormControl type="email" placeholder="Email" />
</Col>
</FormGroup>
<FormGroup controlId="formHorizontalPassword">
<Col componentClass={ControlLabel} sm={2}>
Password
</Col>
<Col sm={10}>
<FormControl type="password" placeholder="Password" />
</Col>
</FormGroup>
<FormGroup>
<Col smOffset={2} sm={10}>
<Checkbox>Remember me</Checkbox>
</Col>
</FormGroup>
<FormGroup>
<Col smOffset={2} sm={10}>
<Button onClick={(event) => handleClick(event)} type="submit">Sign in</Button>
</Col>
</FormGroup>
{errorMessage &&
<p style={{color:'red'}}>{errorMessage}</p>
}
</Form>
)
}
}
export default Login;

Moving handleClick inside the class fixes the error:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {Button, Checkbox, Col, ControlLabel, Form, FormControl, FormGroup} from 'react-bootstrap';
import './Login.css';
Login.propTypes = {
onLoginClick: PropTypes.func.isRequired,
errorMessage: PropTypes.string
};
class Login extends Component {
handleClick(event)
{
const username = this.refs.username;
const password = this.refs.password;
const creds = {
username: username.value.trim(),
password: password.value.trim()
};
this.props.onLoginClick(creds)
}
render() {
const { errorMessage } = this.props;
return (
<Form horizontal>
<FormGroup controlId="formHorizontalEmail">
<Col componentClass={ControlLabel} sm={2}>
Email
</Col>
<Col sm={10}>
<FormControl type="email" placeholder="Email" />
</Col>
</FormGroup>
<FormGroup controlId="formHorizontalPassword">
<Col componentClass={ControlLabel} sm={2}>
Password
</Col>
<Col sm={10}>
<FormControl type="password" placeholder="Password" />
</Col>
</FormGroup>
<FormGroup>
<Col smOffset={2} sm={10}>
<Checkbox>Remember me</Checkbox>
</Col>
</FormGroup>
<FormGroup>
<Col smOffset={2} sm={10}>
<Button onClick={(event) => this.handleClick(event)} type="submit">Sign in</Button>
</Col>
</FormGroup>
{errorMessage &&
<p style={{color:'red'}}>{errorMessage}</p>
}
</Form>
);
}
}
export default Login;

Related

Getting has been blocked by CORS policy error

The below spring boot code is for controller
#CrossOrigin(origins="*")
#RestController
#RequestMapping("/flights")
public class FlightController {
#Autowired
private FlightService flightService;
#GetMapping("/getall")
public List<FlightsDTO> customer(){
return flightService.getFlights();
}
#GetMapping("/get/{flightId}")
public FlightsDTO CustomerbyId(#PathVariable Integer flightId) {
return flightService.getFlight(flightId);
}
#PostMapping("/save")
public ResponseEntity<FlightsDTO> save(#Valid #RequestBody FlightsDTO prod) {
return new ResponseEntity<FlightsDTO>(flightService.createFlight(prod), HttpStatus.CREATED);
}
#GetMapping("/search")
public List<FlightsDTO> searchProduct(#RequestParam("query") String query){
return flightService.searchFlight(query);
}
#PatchMapping("/partialupdatesourceName/{flightId}/{sourceName}")
public ResponseEntity<FlightsDTO> partialUpdateFlightSource(#PathVariable Integer flightId,#PathVariable String sourceName){
return new ResponseEntity<>(flightService.partialupdateFlightSource(flightId, sourceName),HttpStatus.OK);
}
#PatchMapping("/partialupdatedestinationName/{flightId}/{destinationName}")
public ResponseEntity<FlightsDTO> partialUpdateFlightDestination(#PathVariable Integer flightId,#PathVariable String destinationName){
return new ResponseEntity<>(flightService.partialupdateFlightDestination(flightId, destinationName),HttpStatus.OK);
}
#PatchMapping("/partialupdateprice/{flightId}/{price}")
public ResponseEntity<FlightsDTO> partialUpdateFlightPrice(#PathVariable Integer flightId,#PathVariable Long price){
return new ResponseEntity<>(flightService.partialupdateFlightPrice(flightId, price),HttpStatus.OK);
}
}
The below is the react code
import React, {Component} from "react";
import {Card, Form, Button, Col, Row} from "react-bootstrap";
import { FontAwesomeIcon } from "#fortawesome/react-fontawesome";
import {faSave, faPlusSquare, faUndo} from "#fortawesome/free-solid-svg-icons"
import axios from "axios";
export default class Flights extends Component {
constructor(props){
super(props);
this.state = this.initialState;
this.flightChange = this.flightChange.bind(this);
this.submitFlight = this.submitFlight.bind(this);
}
initialState = {
flightId:'', companyName:'', sourceName:'', destinationName:'', price:''
}
resetFlight = () => {
this.setState(() => this.initialState);
}
submitFlight = event =>{
event.preventDefault();
const flight = {
flightId: this.state.flightId,
companyName: this.state.companyName,
sourceName: this.state.sourceName,
destinationName: this.state.destinationName,
price: this.state.price
};
axios.post("http://localhost:9092/flights/save",flight)
.then(response => {
if(response.data != null){
this.setState(this.initialState);
alert("Flight Saved Successfully");
}
});
}
flightChange = event =>{
this.setState({
[event.target.name]:event.target.value
});
}
render(){
const {flightId, companyName, sourceName, destinationName, price} = this.state;
return(
<Card className={"border border-dark bg-dark text-white"}>
<Card.Header>
<FontAwesomeIcon icon={faPlusSquare}/> Add New Flight
</Card.Header>
<Form onReset={this.resetFlight} onSubmit={this.submitFlight} id="flightFormId">
<Card.Body>
<Row>
<Form.Group as={Col} controlId="formGridFIGHTID">
<Form.Label>Flight ID</Form.Label>
<Form.Control required autoComplete="off"
type = "test" name = "flightId"
value={flightId}
onChange={this.flightChange}
className={"bg-dark text-white"}
placeholder="Enter the Flight Id" />
</Form.Group>
</Row>
<Row>
<Form.Group as={Col} controlId="formGridCompanyName">
<Form.Label>Company Name</Form.Label>
<Form.Control required autoComplete="off"
type = "test" name = "companyName"
value={companyName}
onChange={this.flightChange}
className={"bg-dark text-white"}
placeholder="Enter the Company Name" />
</Form.Group>
</Row>
<Row>
<Form.Group as={Col} controlId="formGridSourceName">
<Form.Label>Source Name</Form.Label>
<Form.Control required autoComplete="off"
type = "test" name = "sourceName"
value={sourceName}
onChange={this.flightChange}
className={"bg-dark text-white"}
placeholder="Enter the Source Name" />
</Form.Group>
</Row>
<Row>
<Form.Group as={Col} controlId="formGridDestinationName">
<Form.Label>Destination Name</Form.Label>
<Form.Control required autoComplete="off"
type = "test" name = "destinationName"
value={destinationName}
onChange={this.flightChange}
className={"bg-dark text-white"}
placeholder="Enter the Destination Name" />
</Form.Group>
</Row>
<Row>
<Form.Group as={Col} controlId="formGridPrice">
<Form.Label>Price</Form.Label>
<Form.Control required autoComplete="off"
type = "test" name = "price"
value={price}
onChange={this.flightChange}
className={"bg-dark text-white"}
placeholder="Enter the Price" />
</Form.Group>
</Row>
</Card.Body>
<Card.Footer style={{"textAlign":"right"}}>
<Button size="sm" variant="success" type="submit">
<FontAwesomeIcon icon={faSave} /> Submit
</Button>
<Button size="sm" variant="info" type="reset">
<FontAwesomeIcon icon={faUndo} /> Reset
</Button>
</Card.Footer>
</Form>
</Card>
);
}
}
After Running the server and submitting the values console is showing this error
error message
I have tried multiple solutions like changed the Cross origin annotation from * to the http://localhost:3000 and also add webMVCConfig but found no luck.
How To resolve this issue? btw I have also given Security to the controller, maybe because of that I am facing this issue.

Attempted Import error: 'SomeModule' is not exported from './some/path';

Suddenly, I am getting this error while running my app.
I have imported FloatingLabel from react-bootstrap only which is correct as far as I know.
My code is
import React from 'react';
import { Form, Button, Row, Col, Container, FloatingLabel} from 'react-bootstrap';
import './login.css';
class LoginForm extends React.Component
{
FormExample()
{
return (
<Container fluid="sm">
<Row lg={12}>
<Col lg={6} className="m-auto border border-2 border-dark box-shadow">
<Container className="text-center mt-2">
<h2 className="text-center">Login</h2>
<p>Sign into your account here</p>
</Container>
<Form noValidate aria-label="Login">
<Row md={12} className="mb-3">
<Form.Group as={Col} md={10} className="m-auto" controlId="formGroupPassword">
<FloatingLabel label="Password" controlId="password">
<Form.Control type="password" placeholder="Password" required/>
</FloatingLabel>
</Form.Group>
</Row>
<Row md={12} className="mb-3" >
<Form.Group as={Col} md={10} className="m-auto">
<Form.Check required name="terms" label="Agree to terms and conditions" id="terms"/>
</Form.Group>
</Row>
</Form>
</Col>
</Row>
</Container>
);
}
render()
{
return(this.FormExample());
}
}
export default LoginForm;
Can somebody help me understand why I am getting this error even if I have imported the proper module?
https://react-bootstrap.netlify.app/components/forms/#floating-label-props
Try this^.
import FloatingLabel from 'react-bootstrap/FloatingLabel'

React Send form data to email

I am using create-react-app or my project. I created a form field where I store the data into the state. I would like to send the data as an email, and I'm lost on how to do so.
One of the problems is using create-react-app, I'm not sure where to find my router or server page in node.
Any help would be greatly appreciated.
import React, { Component } from 'react';
import { Button, Col, Grid, Row, Form, FormGroup, FormControl,
ControlLabel, Checkbox } from 'react-bootstrap';
export class Contact extends Component {
constructor(props) {
super(props);
this.state = {
name: "",
email: "",
message: ""
}
}
onChange = (e) => {
const state = this.state;
state[e.target.name] = e.target.value;
this.setState(state);
}
onSubmit = (e) => {
e.preventDefault();
const { name, email, message } = this.state;
}
render() {
const { name, email, message } = this.state;
return(
<div>
<Grid>
<Row id="contactForm">
<Col xs={2}>
</Col>
<Col xs={8}>
<Form horizontal onSubmit={this.onSubmit}>
<FormGroup >
<Col componentClass={ControlLabel} sm={2}>
Name
</Col>
<Col sm={10}>
<FormControl required value={name} name="name" type="name"
placeholder="Name" onChange={this.onChange} />
</Col>
</FormGroup>
<FormGroup controlId="formHorizontalPassword">
<Col componentClass={ControlLabel} sm={2}>
Email
</Col>
<Col sm={10}>
<FormControl required value={email} name="email" type="email"
placeholder="Email" onChange={this.onChange}/>
</Col>
</FormGroup>
<FormGroup>
<Col componentClass={ControlLabel} bsSize="lg" sm={2}>
Message
</Col>
<Col sm={10}>
<FormControl required value={message} name="message" componentClass="textarea" style={{ height: 200 }}
type="message" placeholder="Insert message here" onChange={this.onChange}/>
</Col>
</FormGroup>
<FormGroup>
<Col smOffset={2} sm={10}>
<Checkbox>Flag as important</Checkbox>
</Col>
</FormGroup>
<FormGroup>
<Col smOffset={2} sm={10}>
<Button type="submit">
Send
</Button>
</Col>
</FormGroup>
</Form>
</Col>
<Col xs={2}>
</Col>
</Row>
</Grid>
</div>
)
}
}
Short answer is that You cannot send mail from the client. You will need to have a server to do so. If you have an api service that your React is consuming from, then it is very possible from there.

How to manage state with complex json object in react js

Below is the code I have so far, my state isn't working as expected with a bit complex object, I was able to work with a simple object with no nested objects in my state and its working as expected.
state assignment works fine if I don't use personalInfo or contactInfo inside the customer object and used all the properties and create simple customer object
import React, {PropTypes} from 'react';
import {Grid,Row,Col,Button,Label} from 'react-bootstrap';
import {connect} from 'react-redux';
import * as customerActions from '../../actions/customerActions';
import {bindActionCreators} from 'redux';
class AddCustomer extends React.Component{
constructor(props , context){
super(props , context);
this.state={
customer:{
personalInfo:{
firstName:"",
lastName:"",
dob:"",
ssn:""
},
contactInfo:{
address:"",
addressLine2:"",
city:"",
state:"",
zip:"",
phoneNumber:"",
emailAddress:""
}
}
};
this.handleChange = this.handleChange.bind(this);
this.onClickCancel = this.onClickCancel.bind(this);
this.onClickSave = this.onClickSave.bind(this);
}
handleChange(event) {
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;
this.setState({
customer: {
personalInfo:{
},
contactInfo:{
},
...this.state.customer,
[name]: value
}
});
console.log(this.state.customer);
}
onClickCancel(){
}
onClickSave(){
this.props.actions.createCustomer(this.state.customer);
}
render() {
return (
<div className="container-fluid">
<Grid>
<Row className="show-grid">
<Col xs={10} md={10}><b>ADD A LEAD</b></Col>
<Col xs={2} md={2}>
<Button>×</Button>
</Col>
</Row>
<Row className="show-grid">
<Col xs={12} md={12}>
<hr/>
</Col>
</Row>
<p><b>Personal Information</b></p>
<br/>
<Row className="show-grid">
<Col xs={6} md={6}>
<Row><Label>First Name*</Label></Row>
<Row><input name="personalInfo.firstName" type="text" value={this.state.customer.personalInfo.firstName} onChange={this.handleChange} required="true"/></Row>
</Col>
<Col xs={6} md={6}>
<Row><Label>Last Name*</Label></Row>
<Row><input name="personalInfo.lastName" type="text" value={this.state.customer.personalInfo.lastName} onChange={this.handleChange} required="true"/></Row>
</Col>
</Row>
<br/>
<Row className="show-grid">
<Col xs={2} md={2}>
<Row><Label>Date of Birth*</Label></Row>
<Row><input name="personalInfo.dob" type="text" value={this.state.customer.personalInfo.dob} onChange={this.handleChange} required="true"/></Row>
</Col>
<Col xs={2} md={2}>
<Row><Label>SSN*</Label></Row>
<Row><input name="personalInfo.ssn" type="text" value={this.state.customer.personalInfo.ssn} onChange={this.handleChange} required="true"/></Row>
</Col>
</Row>
<br/>
<p><b>Contact Information</b></p>
<br/>
<Row className="show-grid">
<Col xs={6} md={6}>
<Row><Label>Address</Label></Row>
<Row><input name="contactInfo.address" type="text" value={this.state.customer.contactInfo.address} onChange={this.handleChange}/></Row>
</Col>
<Col xs={6} md={6}>
<Row><Label>Address Line 2</Label></Row>
<Row><input name="contactInfo.addressLine2" type="text" value={this.state.customer.contactInfo.addressLine2} onChange={this.handleChange}/></Row>
</Col>
</Row>
<br/>
<Row className="show-grid">
<Col xs={2} md={2}>
<Row><Label>City</Label></Row>
<Row><input name="contactInfo.city" type="text" value={this.state.customer.contactInfo.city} onChange={this.handleChange}/></Row>
</Col>
<Col xs={2} md={2}>
<Row><Label>State</Label></Row>
<Row><input name="contactInfo.state" type="text" value={this.state.customer.contactInfo.state} onChange={this.handleChange}/></Row>
</Col>
<Col xs={2} md={2}>
<Row><Label>Zip</Label></Row>
<Row><input name="contactInfo.zip" type="text" value={this.state.customer.contactInfo.zip} onChange={this.handleChange}/></Row>
</Col>
</Row>
<br/>
<Row className="show-grid">
<Col xs={4} md={4}>
<Row><Label>Phone Number</Label></Row>
<Row><input name="contactInfo.phoneNumber" type="text" value={this.state.customer.contactInfo.phoneNumber} onChange={this.handleChange}/></Row>
</Col>
<Col xs={4} md={4}>
<Row><Label>Email Address</Label></Row>
<Row><input name="contactInfo.emailAddress" type="text" value={this.state.customer.contactInfo.emailAddress} onChange={this.handleChange}/></Row>
</Col>
</Row>
<br/>
<Row className="show-grid">
<hr/>
</Row>
<Row className="show-grid">
<input className="pull-right" type="submit" onClick={this.onClickCancel} value="Cancel"/>
<input className="pull-right" type="submit" bsStyle="primary" onClick={this.onClickSave} value="Add"/>
</Row>
</Grid>
</div>
);
}
}
AddCustomer.propTypes={
actions:PropTypes.object.isRequired,
customer:PropTypes.array.isRequired
};
function mapStateToProps(state, ownProps) {
return{
customer:state.customer
};
}
function mapDispatchToProps(dispatch) {
return{
actions: bindActionCreators(customerActions,dispatch)
};
}
export default connect(mapStateToProps,mapDispatchToProps)(AddCustomer);
I'm not sure what's the unxepcted result do you receive, but I gues you may try something like this:
handleChange(type, name, event) {
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
this.setState({
customer: {
...this.state.customer,
[type]:{
...this.state.customer[type],
[name]: value
}
}
});
console.log(this.state.customer);
}
...
render() {
...
<Row><input type="text" value={this.state.customer.personalInfo.firstName} onChange={this.handleChange.bind(this, 'personalInfo', 'firstName')} required="true"/></Row>
...
<Row><input type="text" value={this.state.customer.contactInfo.address} onChange={this.handleChange.bind(this, 'contactInfo', 'address')}/></Row>
...
}

react+redux with react-bootstrap > FormControl > get value

I am using react+redux with react-bootstrap components.
I would like to pass the value of a FormControl text element (email) to the dispatched redux action but I do not know how to do that.
class LoginForm extends React.Component {
render() {
const email = React.findDOMNode(this.refs.email);
return (
<div>
<Form horizontal>
<FormGroup controlId="formHorizontalEmail">
<Col componentClass={ControlLabel}>Email</Col>
<Col><FormControl type="email" ref="email"/></Col>
</FormGroup>
<FormGroup>
<Col>
<Button type="submit" block>Sign in</Button>
</Col>
</FormGroup>
</Form>
<Button onClick={() => this.props.doLogin(email, 'password')}>Login</Button>
</div>
)
}
}
/**
* Connect staff.
*/
const mapStateToProps = (state) => {
return {
...
};
};
const mapDispatchToProps = (dispatch) => {
return {
doLogin: (email, password) => dispatch(performLogin(email, password))
};
};
export default connect(mapStateToProps, mapDispatchToProps)(LoginForm)
The only one way how to read text value of a FormControl with React (according to my research) is this:
class LoginForm extends React.Component {
handleOnChange = (event) => {
this.setState({ [event.target.id]: event.target.value }, null);
}
render() {
return (
<div>
<Form horizontal>
<FormGroup controlId="email">
<Col componentClass={ControlLabel}}>Email</Col>
<Col>
<FormControl type="email" placeholder="Email" onChange={this.handleOnChange}
/>
</Col>
</FormGroup>
<FormGroup controlId="password">
<Col componentClass={ControlLabel} sm={2}>Password</Col>
<Col>
<FormControl type="password" placeholder="Password" onChange={this.handleOnChange} />
</Col>
</FormGroup>
<FormGroup>
<Col>
<Button onClick={() => this.props.doLogin(this.state.email, this.state.password)}>Submit</Button>
</Col>
</FormGroup>
</Form>
</div>
)
}
}

Resources