FieldArray remove and push immediately cause view not rerender
See original GitHub issueI 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:
- Created 7 years ago
- Reactions:2
- Comments:11 (3 by maintainers)
Top 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 >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 FreeTop 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
Top GitHub Comments
…and so your code breaks now…
https://github.com/erikras/redux-form/issues/2740
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.