I am building a login form and I am dynamically rendering the input fields using reactjs array map. However, I need to get each user input via the input values. I use states to get the values of each user, but I don't know how to do it dynamically.
Here is my code:
import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom';
import $ from 'jquery';
const loginForm = [
{
name: 'email',
placeholder: 'Email address',
type: 'text'
},
{
name: 'password',
placeholder: 'Password',
type: 'password'
}
]
export class Login extends Component {
constructor(props) {
super(props);
this.state = {
email: '',
password: ''
}
this.getInput = this.getInput.bind(this);
}
getInput = (event) => {
this.setState({
[event.target.name] : [event.target.value]
});
}
resetForm = (event) => {
this.setState({
email: '',
password: ''
});
}
validateForm = (email,password) => {
if(email == "" || password == "") {
return false;
} else {
return true;
}
}
render() {
return (
<div>
<form method="POST" onSubmit={event => this.Login(event)}>
{loginForm.map( (form,index) => (
<div className="form-group">
<input type={form.type} name={form.name} className="form-control" placeholder={form.placeholder} value={this.state.form.name} onChange={event => this.getInput(event)}/>
</div>
))
}
<button type="submit">Sign in</button>
</form>
</div>
);
}
}
dynamically...