DEV Community

Cover image for useState Best Practice
Shaban-Eissa
Shaban-Eissa

Posted on

useState Best Practice

let's see in this post how we can apply best practice for useState()

suppose we have many inputs and we need to make state for each input, normal way to make this will be like :

Image description

This is avoidable because it will consume more time and the code will be un-readable; so the another way and best practice to make is to create state as object and every key is the name of each input such as
const [inputs,setInput] = useState({
name : "" ,
email : "" ,
password : ""})

<input name="name" />
<input name="email" />
<input name="password" />

and there is full code with only one function called handleChange that can handle all inputs

Image description

Top comments (0)