Well it's not just you, I also fail to grasp how this works. The idea seems nice, but the author could have elaborated just a tiny little bit more for the mere mortals among us.
Ah on reading it a second time I get it now ... for a second field (let's say "email") you'd just do:
const {reset, ...email} = useField('text')
so for those 2 fields you'd have (we need to "rename" the 'reset' member during destructuring):
and so on ... the only thing I don't get is why we need the "type" parameter to the hook: useField('text') ... and we probably also don't need the "id" attributes (id='username' and id='email').
So, this might work equally well (and save some typing) ... ?
Yeah absolutely, we need not have multiple onChange handlers either. The type parameter is for the input element's type attribute is required to mention what type of input it is, could be email, password, text, number, range etc.
I came to the same conclusion, but if this is true, then now I have multiple useFields instead of useStates, it is just omitting value and onChange. About the Type parameter, I think he wanted to also set the input type.
Yes you'd have multiple useFields, I think that's clearly the idea.
But it's not omitting value and onChange - instead, value and onChange are being expanded as props on the elements, so you're effectively linking the attributes on the input elements with function closures which are generated by the "useField" calls.
I still don't see the point of the type param, unless you'd end up with just:
<input {...email}/>
where the 'type' attribute is then also generated through the object expansion (the ... ellipsis).
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Well it's not just you, I also fail to grasp how this works. The idea seems nice, but the author could have elaborated just a tiny little bit more for the mere mortals among us.
Ah on reading it a second time I get it now ... for a second field (let's say "email") you'd just do:
so for those 2 fields you'd have (we need to "rename" the 'reset' member during destructuring):
and so on ... the only thing I don't get is why we need the "type" parameter to the hook:
useField('text')
... and we probably also don't need the "id" attributes (id='username'
andid='email'
).So, this might work equally well (and save some typing) ... ?
Other than that, yeah pretty clever and elegant.
Yeah absolutely, we need not have multiple onChange handlers either. The type parameter is for the input element's type attribute is required to mention what type of input it is, could be email, password, text, number, range etc.
I came to the same conclusion, but if this is true, then now I have multiple useFields instead of useStates, it is just omitting value and onChange. About the Type parameter, I think he wanted to also set the input type.
Yes you'd have multiple useFields, I think that's clearly the idea.
But it's not omitting value and onChange - instead, value and onChange are being expanded as props on the elements, so you're effectively linking the attributes on the input elements with function closures which are generated by the "useField" calls.
I still don't see the point of the type param, unless you'd end up with just:
where the 'type' attribute is then also generated through the object expansion (the ... ellipsis).