question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Arrays of objects cause multiple re-renders and lose focus on change.

See original GitHub issue

Bug

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:closed
  • Created 6 years ago
  • Reactions:16
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

48reactions
nmartinezb3commented, Aug 14, 2018

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 the map inside the array field. Make sure always setting the same key value to the same node.


<FieldArray 
  name={`resources.${index}.linkList`}
  render={arrayHelpers => (
    {values.linkList.map(link => (
       <div key={link.key}>....</div>
     )}
    <button onClick={() => arrayHelpers.push({ key: Math.random(), link: '' })} >
      Add link
    </button>
  )}
 />


6reactions
jafricommented, Mar 30, 2019

Issue still exists with using a HOC

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found