formik: FastField does not avoid re-renders
š Bug report
Current Behavior
Using FastField
components, changes on other fields end up re-rendering the whole form, including other FastField
s.
Hereās a demonstration running the documentation example, where āFirst Nameā is a FastField
and āEmailā is a regular Field
.
- Updates to āFirst Nameā (
FastField
)
- Updates to unrelated regular
Field
Additionally, the code snippet in docs above does not compile as-is: itās accessing an undeclared
form
variable in therender
method ofFormik
.
Expected behavior
Changes on a Field
should not trigger re-renders of unrelated FastField
s.
Reproducible example
https://codesandbox.io/s/nrzq1vv1p (a simple, amended copy-paste of example from docs).
Your environment
Software | Version(s) |
---|---|
Formik | 1.4.3 |
React | 16.7.0 |
TypeScript | - |
Browser | Chrome 72.0.3626.96 |
npm/Yarn | yarn 1.13.0 |
Operating System | macOS 10.14.2 (18C54) |
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 2
- Comments: 16 (3 by maintainers)
@jaredpalmer Why close this? Weāve shown thereās room for improvement here and
FastField
does not avoid all re-renders.RenderFunction
(I mean<Formik render={() => RenderFunction} />
) always re-renders, I mean always whenvalues
,touched
,errors
etc. change. And we cannot memoize it (I mean wrap inReact.memo
). I have a hack for it: I makeRenderFunction
return another component (<AnotherComp {...props} />
) which is wrapped inReact.memo
. That is how I prevent whole-form re-renders⦠Anyway maybe it works as it should.Debounced HOC code for inputs.
Iāve been having problems with FastField, on Formik
2.1.4.
Changes from other Fields will force rerenders of all FastFields. I made this HOC which takes a normal input (only tested with text or textarea) and wires it up to Formik in a debounced manner. Essentially, when the input is focused and the user is typing, it becomes an uncontrolled component, and every few seconds it syncs back up to the main formik value store by executing change handlers. When itās not in focus, the input acts as a normal controlled component.Same problem. I donāt get itā¦
Ok, follow up here.
After closer inspection, I realised that all re-renders were being caused by the
connect
HoC. Wrapping the component it returns inReact.memo
makes all why-did-you-update warnings go away.@nfantone Iām just getting started with Formik and was under the same impression. But the highlight updates can be misleading since you have to consider the component tree.
After using the profiler, I think FastField is doing its job:
One thing that Iām not understanding is why do we get 4 commits in the profiler. It seems to be related to validation. Hereās the data for FirstName (FastField):
The isValidating prop changed twice, and that seems to be the reason of the 4 commits reported by the profiler.
Is it expected for the validation to run twice?
Thanks