-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot use a higher-order function in Field Validation #3288
Comments
Just reverted back to 6.8.0 and this works as expected. |
This is expected behaviour see #3271. |
@danielrob Cool. I followed through on the threads and updated docs, but I'll be honest. This is still a very confusing change to me. I guess I need to switch to using a bound method instead of a lambda created at render time, but I'm still not really sure. |
I fully admit my documentation update for this was minimal... and that while this change makes the library more powerful, it also makes the consumption more advanced. Your help would be appreciated to alleviate this... perhaps you could think about where in the documentation / how this could be explained to make it clear? Edit: I hadn't actually realised that this affects all inline validation functions regardless of whether they were higher order functions or not. See the other ticket. |
Could you please enlighten me here? What's the cause for this not to work? I didn't get it yet. |
@gabrielhpugliese ok the latest: So either (option 1):
Or something like (option 2):
Or (option 3)
Please note I'm just doing this off the top of my head, and I'm primarily an issues helper and haven't tried this on my personal project yet, so this is just my understanding presently. |
I'm more than just "an issues helper". 😄 Two things:
Creating a new function for a prop in
|
Thanks for the info! What if I depend on a prop? What's the correct way to approach in a stateless component? For example:
I really didn't want to couple required function to the translate function. |
I also have another example where I have a range and the message should be something like "Choose between 1 and 10" but the range is configurable and dynamic. I'm kinda struggling with that. |
I'd tweak the wording to be super explicit there:
BUT@erikras in the HOF case described in this issue, this doesn't fly: Suppose I have a field definition which somewhere up the chain get's passed as a prop
And I want to create a max 5 validator where the max is 5 from Well, if The whole point of this change is that you can change a validator function on a mounted component:
Otherwise we need to change the
And have Unless I'm somehow mistaken - please enlighten me! Relevant reading confirming the anti-pattern statement: Binding parent props to passed function: Passing just any on the fly made function in general: @duro I'm going to humbly apologize and re-open this as it clearly warrants more discussion... but I'll throw the discussion tag on it. |
I think that @gabrielhpugliese has a good point. If I wanted the error message to change based on props, defining it once outside the render would limit that. That seems like a legit use case. |
@duro, @gabrielhpugliese's case is precisely the same as your case except that |
But I don't think optimizations should be done for me in this case. There
should be an API where I can optimize. This is too obtrusive IMO.
…On Aug 9, 2017 8:04 AM, "Daniel Robinson" ***@***.***> wrote:
@duro <https://github.com/duro>, @gabrielhpugliese
<https://github.com/gabrielhpugliese>'s case is precisely the same as
your case except that 'Message Override' is the result of calling trans('some
string'). That doesn't change the issue. The problem is that require() is
an HOC.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3288 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABMd0hEbi_iodXT_00W3fHoIPsxwE-G9ks5sWUvQgaJpZM4OtQfy>
.
|
@gabrielhpugliese right so tl;dr the new API as it stands means that you must optimise, you can't generate a new function every render - you must either bind during lifecycle methods/instantiation or use memoization if you have a validator HOF. |
Looks like the cause of this issue is defaultShouldValidate does not check if field level validators have changed. You can see the correct behavior by passing shouldValidate: () => true to reduxForm (Obviously not a great solution). |
If you want to use a higher order function then you need to memoize it so that a new function is not created on every call. This is a very common pattern I use for render level action handlers that need to be parameterized. Using lodash memoize:
|
Is this a good blog post to change your minds about this? https://cdb.reacttraining.com/react-inline-functions-and-performance-bdff784f5578 |
I agree with @gabrielhpugliese, inline functions are "bad" but they are not "omg the app will explode". I am hoping that this behavior is reverted to v6.8. If there is a performance issue, inline functions are the first place to look, but that doesn't mean you have to force it on everyone. The operative word is "if". In my case I tried to add a |
Hi @erikras, I have been all kinds of burning out on my day job lately, so apologies up front for not being more proactive here, but also this validation issue is all kinds of wrong:
This would essentially achieve the same as validator={maxValueValidator(max)}, but without the anti-pattern. There are other solutions, all of which will probably involve half a weeks refactoring when upgrading our project from 6.8.0. For example looking up each schema from the existing formProps. In short: I'm dubious about what #3094 was actually trying to achieve given that it doesn't apparently completely work, and I agree with the consensus in this discussion that the loss of some sneaky anti-pattern usage is a real pain in the neck though I there are better solutions than just reverting. |
@danielrob pre #3094 when a Fields name was updated the new name was registered without the validation functions. #3094 made two changes, including validation functions when the name is updated and re-registering when validation functions changed. Reverting the 're-registering when validation functions change part' may solve some of these issues, but I think another mechanism will be needed to notify the Form component validation needs updating after a change. |
* Do not re-register field when validation functions change * Add fieldDidUpdate callback on _reduxForm to trigger validation updates See issue: redux-form#3288
Any plans on fixing this or reverting to 6.8? |
Hi, migrating to redux-form v7 took me hours because of this change, it was pretty hard to find that passing hoc validation function was the problem. |
I'm closing this in favour of #4017. |
Are you submitting a bug report or a feature request?
Bug
What is the current behavior?
When using field level validators, I am unable to use a higher order function to produce the validation function
This works:
This DOES NOT work:
What is the expected behavior?
I would expect to pass into the field validator a returned function that matches the validator interface even if it was produced by a higher order function
Sandbox Link
https://codesandbox.io/s/G6ARklAMr
What's your environment?
Redux Form: 7.0.3
OS: macOS
Browser: Chrome 60
Other information
The text was updated successfully, but these errors were encountered: