While working with react it is almost inevitable to come across controlled components. A controlled component is a react component that controls th...
For further actions, you may consider blocking this person and/or reporting abuse
Wow! This is the cleanest and easiest I have seen a controlled input done. I have one question. Every other time I've looked up a tutorial on how to do a controlled input, they have always set the input value to the state variable, e.g.
<input type='text' value='startDate' onchange='changeDate' />
How does this work without requiring the value atribute?
Hello Mike! It should work with or without explicitly setting the
value
attribute.Try it yourself by running the source code on repl.it :
repl.it/@StanleyJovel/Controlled-C...
However, the example code you shared doesn't seem to be valid JSX.
Oh, you mean the missing curly braces? Yeah, I forgot to type those, but they are in my code. What I'm stuck on is how to change those input values from another component? I have lifted the state to a shared parent. But when I add in
value={batchDates.startDate}
to my input, I get:Warning: A component is changing an uncontrolled input of type date to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component.
Interesting, Is it okay if I take a look at some of your code?
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.
Awesome 😃
I think the confusion comes from the fact that setting
onChange
in itself doesn't make the input controlled unless you are settingvalue
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.this worked. Thanks Orsi :)
This is a good approach, but this didn't worked for me.
still showing
I think you are not using the value of the state object as the value of input..
this is not the controlled input here.
I'd also like to point out that you could also add additional functionality such as 'setDefaults', or 'clearValues' too.
Just got out of a bootcamp was struggling with handle changes and hooks etc this makes so much sense and I really like how the hooks is in a separate file which allows a such a clean reading ability thank you for this article!!!
Perfect Stanley! :) Thanks!
Stanley, wow small world!! I just saw you graduated in Computer Science from UW-Eau Claire. I graduated in Computer Science from UW-Eau Claire as well!!
Really?! That is so awesome!
I did not graduate there though haha I did a 1-year exchange program and absolutely loved it.
"THIS APPROACH IS NOT SCALABLE"
unless this.setState({[event.target.name]: event.target.value})
;)
Yes, that is why I'm using that exact approach