DEV Community

Discussion on: Simplify controlled components with React hooks

 
stanleyjovel profile image
Stanley Jovel

Interesting, Is it okay if I take a look at some of your code?

Thread Thread
 
dmikester1 profile image
Mike Dodge

OK, nevermind, figured it out! :) I wasn't initializing my state variables correctly. But I think in order to be able to set it from a different component, I will need to have that value attribute set to a state value.

Thread Thread
 
stanleyjovel profile image
Stanley Jovel

Awesome 😃

Thread Thread
 
punkah profile image
Orsi

I think the confusion comes from the fact that setting onChange in itself doesn't make the input controlled unless you are setting value as well.

And you are right, the controlled component value needs to be initialized so the input state should be initialized first: const [input, setInput] = useState({ username: "", password: "" }). These initial values could be passed into the custom hook too.

Thread Thread
 
chandra profile image
Chandra Prakash Tiwari

this worked. Thanks Orsi :)