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

Reuse connected ConnectedField, ConnectedFields and ConnectedFieldArray #1734

Merged
merged 1 commit into from Sep 15, 2016

Conversation

huan086
Copy link
Contributor

@huan086 huan086 commented Sep 9, 2016

Redux connect() returns a new class. Currently, each ConnectedField, ConnectedFields and ConnectedFieldArray is connected to redux, thus creating many classes. This change connects ConnectedField, ConnectedFields and ConnectedFieldArray only once.

In a field array, if we would like to preserve state of our component, we need to use a key that identifies the element, instead of array index. However, doing so, Field, Fields and FieldArray detects that it is now bound to a new field name and discards the old connected class and connect again. React sees that the class is different, thus discards the old virtual DOM, along with the component and its state.

By using mapStateToProps(state, ownProps) and mapDispatchToProps(dispatch, ownProps), we can dynamically map according to the current ownProps.name or ownProps.names, thus ConnectedField, ConnectedFields and ConnectedFieldArray only needs to be connected once. React will see the same class and reuse the virtual DOM, preserving our component and its state.

…ay so

as not to cause component to be recreated, losing its state. Fixes redux-form#1727
@codecov-io
Copy link

Current coverage is 100% (diff: 100%)

Merging #1734 into master will not change coverage

@@           master   #1734   diff @@
=====================================
  Files          52      52          
  Lines        1115    1135    +20   
  Methods         0       0          
  Messages        0       0          
  Branches        0       0          
=====================================
+ Hits         1115    1135    +20   
  Misses          0       0          
  Partials        0       0          

Powered by Codecov. Last update 9186bd3...6f93372

@erikras erikras merged commit 89e183f into redux-form:master Sep 15, 2016
@erikras
Copy link
Member

erikras commented Sep 15, 2016

Published in v6.0.3.

@lock
Copy link

lock bot commented Jun 2, 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 2, 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