Don't repeat the code...
In React, like anywhere else in your code, you must never repeat yourself unless it is strictly necessary (almo...
For further actions, you may consider blocking this person and/or reporting abuse
Yeah, curried functions are perfect for this. The arrow syntax lends itself to this.
becomes
Or just
valueChange = key => e => this.setState({[key]: e.target.value})
Yesssssssssssssss
Love It!
using curried function is a good idea! it's very useful for handling multiple similar state changes.
another approach, if you're dealing with form elements like inputs etc. would be to use
name
attribute, something like this:but of course it works only with certain html elements (
<input>
,<select>
,<textarea>
among them) and IIRC specification saysname
should be lowercase, although I don't think I've run into any problems with camelCasename
so far.I like this, how about leveraging a
data-
attribute instead ofname
?You're creating a new function for every input you manage for every render pass. This is an anti pattern especially on big state trees. Especially because it will cause re-renders of child components unnecessarily (as the onChange prop for each element changes every render pass. You may want to cache the created functions, or take a different approach
But.. have you seen all the post? _^
I think so, what specifically did I overlook? Supplying a better alternative?
There’s a warning about rerendering... and me asking for suggestions ✌🏻
Sorry if it’s not perfect, I’m here for learning too.
No worries, just wanted to chip in. Somehow dont see a note about rerender, I'm on mobile, perhaps some caching going on.
I like the alternative via name prop for input elements, or a custom data prop, as I wrote in the other comment thread.
Alternatively, I would still create a function property per onChange handler, but create and use a reusable function to limit the repetitiveness to the bare minimum.
You can create a curried function and then memoize it. 😉 It should work! Have a look at Lodash's
curry
andmemoize
functions.You can try this alternative instead that doesn't require you to add any instance method (methods declared using arrow fns).
The approach demonstrated in this post to manage form data is known as controlled components, which is traditional "reactish" way to handle input value changes. Since the post is intended for beginners, maybe you could complement it adding a note to mention this for those who'd like to read further on the subject. 🙂
More details can be found here: reactjs.org/docs/uncontrolled-comp...
In the valueChange function where did you find the state variable. May be it should be obj variable ?
In the second example?
The state is the obj variable.
You can see I’m creating an obj and using key-value pairs in It.
Then I’m passing the object to setState().
you defining
obj
, but then you usestate
. in non-strict mode this leads to the creation of a global variable, in strict mode this leads to an error.After that you use
obj
again, so the new value saved instate
in state isn't even used.Understood!