Arrays of objects cause multiple re-renders and lose focus on change.
See original GitHub issueBug
handleChange is losing focus on an array nested item where if the array is an array of objects, it loses focus on the first handleChange called.
Current Behavior
When any field in a nested array is updating (only noticed this when using text inputs vs select boxs) the focus is completely lost as the component fully re-renders anew.
Desired Behavior
Component should maintain focus
Additional Information
Having debugged to replace it with my own handler for handleChange, can confirm is it the use of setFieldValue that is where the component loses focus and control. This makes me think its the re-rendering that makes this break.
<Field name={
friends.${index}.field} handleChange={handleChange} handleBlur={handleBlur} />
Focus is only removed by a command that affects the value.
- Formik Version: 0.11.11
- React Version: 16.2
- TypeScript Version: N/A
- CodeSandbox Link:
- OS: osX
- Node Version: v8.4.0
- Package Manager and Version: 1.4
Issue Analytics
- State:
- Created 6 years ago
- Reactions:16
- Comments:13 (2 by maintainers)
Top Results From Across the Web
React.js - input losing focus when rerendering - Stack Overflow
The loss of focus happens because the component prop uses React.createElement each time instead of just re-rendering the changes. Details here: https:// ...
Read more >React re-renders guide: everything, all at once - Developer way
State changes are the “root” source of all re-renders. ... triggered on every re-render; if a component was focused, focus will be lost....
Read more >Do inline arrays (or objects) cause re-renders? : r/reactjs
In general, yes, whenever you pass an object or array you'll inadvertently cause rerenders. Now, keep in mind, there are two parts to...
Read more >Debug Why React (Re-)Renders a Component
React is extremely careful and re-renders "everything all the same time". Losing information by not rendering after a state change could be ...
Read more >How to stop re-rendering lists in React? - Alex Sidorenko
If the component wrapped with memo re-renders, it means that one of its properties changes. Let's figure out which one. Memoizing properties. We ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is happening to me without any HOC. Somebody has a workaround?
Edit: After investigating for a while, I realized that what was making me loose focus after typing was the wrong
key
on the parent component of themap
inside the array field. Make sure always setting the samekey
value to the same node.Issue still exists with using a HOC