Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FieldArray] Pass reference to the fields prop in map, forEach, and reduce callbacks #2187

Merged

Conversation

Whoaa512
Copy link
Contributor

It's probably not well know but creating functions inline within a render call is a performance hit since new functions are created on each re-render. This in turns clutters our memory space and makes the GC work unnecessarily.

To avoid this it would be helpful if fields.map, fields.forEach, and fields.reduce passed along a fields reference to the callback provided. This allows us to create a callback method that can be passed directly the fields.map while still allowing access to swap, remove, pop, etc.

Example:

 const renderSubFields = (member, index, fields) => (
     <li key={index}>
       <button
         type="button"
         title="Remove Member"
         onClick={() => fields.remove(index)}/>
       <h4>Member #{index + 1}</h4>
       <Field
         name={`${member}.firstName`}
         type="text"
         component={renderField}
         label="First Name"/>
       <Field
         name={`${member}.lastName`}
         type="text"
         component={renderField}
         label="Last Name"/>
     </li>
 )
 const renderMembers = ({ fields }) => (
   <ul>
     <button type="button" onClick={() => fields.push({})}>Add Member</button>
     {fields.map(renderSubFields)}
   </ul>
 )

To allow for the access to `swap`, `remove`, `pop`, etc., without
requiring closure scoping
@codecov-io
Copy link

codecov-io commented Nov 29, 2016

Current coverage is 100% (diff: 100%)

Merging #2187 into master will not change coverage

@@           master   #2187   diff @@
=====================================
  Files          58      58          
  Lines        1147    1148     +1   
  Methods         0       0          
  Messages        0       0          
  Branches        0       0          
=====================================
+ Hits         1147    1148     +1   
  Misses          0       0          
  Partials        0       0          

Powered by Codecov. Last update 439904c...8ca1f2a

@erikras erikras merged commit 1250fd8 into redux-form:master Nov 30, 2016
@erikras
Copy link
Member

erikras commented Nov 30, 2016

Nicely done!

@erikras
Copy link
Member

erikras commented Nov 30, 2016

Published in v6.2.1.

@Whoaa512 Whoaa512 deleted the pass-along-fields-for-FieldArray-map branch November 30, 2016 21:45
@lock
Copy link

lock bot commented Jun 1, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 1, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants