React JS Input From Shotcut
import React, { useState } from 'react';
const Forms = () => {
const [names, setnames] = useState({
fname: '',
lname: '',
email: ''
});
const submitting = (e) => {
e.preventDefault(); //revent default events
}
const inputEvent = (e) => {
// const names2 = e.target.name;
// const value = e.target.value;
const {name, value} = e.target; //array destructure
setnames((preValue) => {
return {
...preValue, //using spred operator
[name]: value, //change the value where name is name
};
// if (names2 === 'fname') {
// return{
// fname: value,
// lname: preValue.lname,
// email: preValue.email
// }
// }
// else if(names2 === 'lname'){
// return{
// fname: preValue.fname,
// lname: value,
// email: preValue.email
// }
// }
// else if(names2 === 'email'){
// return{
// fname: preValue.fname,
// lname: preValue.lname,
// email: value
// }
// }
});
}
/* const clicked = () => {
setfullname(names);
setnewname(lastname);
}*/
return (
<>
<div className="col-6 pt-5 m-auto">
<form onSubmit={submitting}>
<h1 className="mb-3 text-center">Hello {names.fname} {names.lname}</h1>
<p className="text-center">{names.email}</p>
<input className="form-control mb-3" type="text" placeholder="Enter Your Name" onChange={inputEvent} name="fname" />
<input className="form-control mb-3" type="text" placeholder="Enter Your Name" onChange={inputEvent} name="lname" />
<input className="form-control mb-3" type="email" placeholder="Enter Your Email" onChange={inputEvent} name="email" />
</form>
</div>
</>
)
}
//onchange is a input event listener in React.
export default Forms;
//react has two types of components - 1. cotrolled components 2. un-controlled components
মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন