import React, { Component } from 'react' class Form extends Component { constructor(){ super() this.state = { email: "", //-------- (5) password: "", hobby: [] //--------- (12) } } //--------- (4) //input Handler inputHandler = (e) => { this.setState({ [e.target.name] : e.target.value //store the value of the input in state value and update the state value ------------ (3) }) } // ---------- (2) //hobbyController hobbyController = (e) => { if(e.target.checked){ {/* -------- (9) */ } this.setState({ hobby : [...this.state.hobby, e.target.value] //-------- (11) }) }else if(e.target.checked == false){ {/* -------- (10) */ } let index = this.state.hobby.findIndex(ele => ele == e.target.value) this.state.hobby.splice(index, 1) //-------- (13) } } //----------------- (8) //submitHandler submitHandler = (e) => { e.preventDefault() //to avoid auto page load during submission ------------------ (16) let {email, password, hobby} = this.state //get the data from state -------------- (17) console.log(`Email: ${email} Password: ${password} Hobby: ${hobby}`); } // -------------- (15) render() { return (
) } } export default Form