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
valueattribute.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'd also like to point out that you could also add additional functionality such as 'setDefaults', or 'clearValues' too.
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.
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!!!
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.
Perfect Stanley! :) Thanks!
"THIS APPROACH IS NOT SCALABLE"
unless this.setState({[event.target.name]: event.target.value})
;)
Yes, that is why I'm using that exact approach