shopnilSS

Form Handling in Controlled way or React way

Jun 2nd, 2021 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react'
  2.  
  3. class Form extends Component {
  4.     constructor(){
  5.         super()
  6.         this.state = {
  7.             email: "", //-------- (5)
  8.             password: "",
  9.             hobby: [] //--------- (12)
  10.         }
  11.     } //--------- (4)
  12.  
  13.     //input Handler
  14.     inputHandler = (e) => {
  15.         this.setState({
  16.             [e.target.name] : e.target.value //store the value of the input in state value and update the state value ------------ (3)
  17.         })
  18.     } // ---------- (2)
  19.  
  20.     //hobbyController
  21.     hobbyController = (e) => {
  22.         if(e.target.checked){          {/* -------- (9) */ }
  23.             this.setState({
  24.                 hobby : [...this.state.hobby, e.target.value]  //-------- (11)
  25.             })
  26.         }else if(e.target.checked == false){  {/* -------- (10) */ }
  27.             let index = this.state.hobby.findIndex(ele => ele == e.target.value)
  28.             this.state.hobby.splice(index, 1)  //-------- (13)
  29.         }
  30.     } //----------------- (8)
  31.  
  32.     //submitHandler
  33.     submitHandler = (e) => {
  34.         e.preventDefault() //to avoid auto page load during submission ------------------ (16)
  35.         let {email, password, hobby} = this.state //get the data from state  -------------- (17)
  36.         console.log(`Email: ${email}
  37.                     Password: ${password}
  38.                     Hobby: ${hobby}`);
  39.     } // -------------- (15)
  40.  
  41.     render() {
  42.         return (
  43.             <form>
  44.                 {/* email  */}
  45.                 <div className="form-group">
  46.                     <label htmlFor="exampleInputEmail1">Email address</label>
  47.                     <input type="email" className="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email" name = "email" value = {this.state.email} onChange = {this.inputHandler}></input> {/* ---------- (1) */}
  48.                 </div>
  49.  
  50.                 {/* password  */}
  51.                 <div className="form-group">
  52.                     <label htmlFor="exampleInputPassword1">Password</label>
  53.                     <input type="password" className="form-control" id="exampleInputPassword1" placeholder="Password" name = "password" value = {this.state.password} onChange ={this.inputHandler}></input>
  54.                 </div>
  55.  
  56.  
  57.                 {/* hobby  */}
  58.                 <label htmlFor="hobby">Hobby</label>
  59.                     {/* drawing  */}
  60.                 <div className="form-check">
  61.                     <input type="checkbox" className="form-check-input" id="exampleCheck1" name = "hobby" value = "drawing" onChange = {this.hobbyController}></input> {/* ---------- (7) */}
  62.                     <label className="form-check-label" htmlFor="Drawing">Drawing</label>
  63.                 </div>
  64.                     {/* playing  */}
  65.                 <div className="form-check">
  66.                     <input type="checkbox" className="form-check-input" id="exampleCheck1" name = "playing" value = "playing" onChange = {this.hobbyController}></input> {/* ---------- (6) */}
  67.                     <label className="form-check-label" htmlFor="Playing">Playing</label>
  68.                 </div>
  69.  
  70.                 {/* submit  */}
  71.                 <button type="submit" className="btn btn-primary" onClick = {this.submitHandler} >Submit</button> {/* ---------- (14) */}
  72.             </form>
  73.         )
  74.     }
  75. }
  76.  
  77. export default Form
Add Comment
Please, Sign In to add comment