DEV Community

Discussion on: I was creating Forms the wrong way all along in React.js 🤔

Collapse
 
dank1368 profile image
DanK1368

What about handling forms like this?

const [values, setValues] = useState({
first_name: "",
last_name: "",
email: "",
})

const handleInputValues = (e) => {
    setValues(prevState => {
       return { ...prevState, [e.target.name]: e.target.value }
    })
}

<input type="text" name="first_name" onChange={handleInputValues}/>
<input type="text" name="last_name" onChange={handleInputValues}/>
<input type="text" name="email" onChange={handleInputValues} />
Enter fullscreen mode Exit fullscreen mode

I've been using this method for some time now, without any issues