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.

FieldArray remove and push immediately cause view not rerender

See original GitHub issue

I have a method that remove a field from FieldArray and immediately add a field into FieldArray but the view is not rerender. The state in redux is already updated to the new field when I look at the redux logger log.

  fields.remove(0);
  fields.push({ postId: 1 });

When I press on one of the input in the FieldArray, the FieldArray will only rerender.

After I read through the source code of ConnectedFieldArray.js, I found that the shouldComponentUpdate method is preventing the FieldArray to rerender

const propsToNotUpdateFor = [
  '_reduxForm',
  'value'
]
    shouldComponentUpdate(nextProps) {
      const nextPropsKeys = Object.keys(nextProps)
      const thisPropsKeys = Object.keys(this.props)
      return nextPropsKeys.length !== thisPropsKeys.length || nextPropsKeys.some(prop => {
        // useful to debug rerenders
        // if (!plain.deepEqual(this.props[ prop ], nextProps[ prop ])) {
        //   console.info(prop, 'changed', this.props[ prop ], '==>', nextProps[ prop ])
        // }
        return !~propsToNotUpdateFor.indexOf(prop) && !deepEqual(this.props[ prop ], nextProps[ prop ])
      })
    }

the value of the FieldArray’s Field has changed, but it is returning false due to this part" !~propsToNotUpdateFor.indexOf(prop)"

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
stjepancommented, Mar 27, 2017

…and so your code breaks now…

https://github.com/erikras/redux-form/issues/2740

1reaction
dtipsoncommented, Feb 8, 2017

Hah, but that just made me realize (I do have the values if I want, but that’s not what I want) I do have access to fields.length, and since it’s removing that’s causing the problem, I can just use that as the key: key={${i}_${fields.length}}

That solves the issue, at least for adding/removing. It probably requires re-rendering every item (because the keys change) but that’s acceptable in my case, and maybe the best I can do without a uuid system built into the data.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Formik FieldArray not rerendering on updated values even ...
I'm using this in a Formik FieldArray component. I'm updating the quantity in a button onClick handler using an inline arrow function that ......
Read more >
FieldArray - Redux Form
Returns the item removed. This is not a mutator; it dispatches an action which updates the state in Redux, which will cause your...
Read more >
React FieldArray Form Tutorial: Using Formik, Yup ... - YouTube
In this tutorial, we'll build a donations platform using the Formik FieldArray form with React and Yup for validation.In order to validate ...
Read more >
Examples - Final Form Docs
Demostrates how to use the <FieldArray/> component, from react-final-form-arrays , to render an array of inputs, as well as use push , pop...
Read more >
How To Manage State with Hooks on React Components
Start by creating a component with no state. ... When you do, the browser will refresh and you'll see the Product component: Product...
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