Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't type text into text fields in React

This is the ‘add employee’ page in the employee management system. Here I created a form using react but I can’t enter or type texts to these input text fields. How can I solve this?

const AddEmployee=()=>{
    const [state, setState]=useState(initialState);
    const{firstName,lastName,emailId}= state;
    const addEmployee = async(data)=>{
        EmployeeService.createEmployee(data);
        
    };
    const handleInputChange = (e) =>{
        let { firstName, value  }= e.target;

        setState({ ...state, [firstName]:value});
    }

    return (
        <div>
                   <form>
                                
                               <label htmlFor="firstName"> First Name: </label>
                               <input type="text" placeholder="First Name" name="firstName" className="form-control" 
                                           value={firstName} onChange={handleInputChange} required/>   
                                    <button className="btn btn-success" onClick={handleSubmit}>Save</button>
                                     </form>
                            </div>
                        
    );
}

export default AddEmployee;

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You want to extract name from e.target not "firstName" that does not exist on the e.target element:

const handleInputChange = (e) =>{
    let { name, value  }= e.target;
    setState({ ...state, [name]:value});
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading