-
-
Notifications
You must be signed in to change notification settings - Fork 482
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
re-initialize onFocus/onBlur handlers if the subscribed field changes #787
re-initialize onFocus/onBlur handlers if the subscribed field changes #787
Conversation
@erikras seems like rollup has dropped support for node8 since 2.0.0: react-final-form's You can easily check this locally by cloning react-final-form and running Should the node8 be removed from Travis CI config, or should rollup be pinned to older version? The codesandbox is also failing to something else, but I do not think it is related to my changes. Any idea? |
61feaae
to
62f6435
Compare
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 62f6435:
|
Codecov Report
@@ Coverage Diff @@
## master #787 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 17 17
Lines 253 253
Branches 59 59
=========================================
Hits 253 253
Continue to review full report at Codecov.
|
@erikras I rebased master and now all the tests pass. Could this be merged? It's really annoying having to hack around this. |
Changing the name of your field while it's mounted seems ill-advised. One could also force an unsubscribe/subscribe cycle with a |
We have a form where you have a list of items you can edit, but only one of the items (the selected one) is displayed at a time. We don't on purpose change the name of the field (the name is coming as a prop from parent), it's just optimization React does when it diffs the VDOMs of previous and next state, it notices that the structure is the same and only some attributes for the elements has changed, so it decides to re-use the existing elements instead of unmounting/re-mounting. The |
Published in |
If you use
useField
(or<Field>
) and the name of the field that you subscribe to changes during the lifetime of the component, theonFocus
handler will continue calling thestate.focus()
function of the previous field.The reason is that
useField
doesn't specify thestate.focus
as a dependency forReact.useCallback
.It is a common use-case with forms that you, for example, have a list of items, and you want to be able to toggle between the active item that you are editing, but not render all the items in DOM, rather just the one you're editing. At the moment in these use-cases when switching between the active item, the
Field
does not re-mount and the handlers will not work properly.