-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RFR] ensure redux-form does not destroy forms after they are remounted
Related to this redux-form bug: redux-form/redux-form#3435 (comment)
- Loading branch information
Showing
2 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { actionTypes } from 'redux-form'; | ||
|
||
const state = {}; | ||
|
||
const handleDestroy = (action, next) => { | ||
state[action.meta.form] = (state[action.meta.form] || 0) - 1; | ||
|
||
if (state[action.meta.form] <= 0) { | ||
return next(action); | ||
} | ||
|
||
// Drop the action | ||
return false; | ||
}; | ||
|
||
const handleInitialize = (action, next) => { | ||
state[action.meta.form] = (state[action.meta.form] || 0) + 1; | ||
return next(action); | ||
}; | ||
|
||
const handleAction = (action, next) => { | ||
switch (action.type) { | ||
case actionTypes.DESTROY: | ||
return handleDestroy(action, next); | ||
case actionTypes.INITIALIZE: | ||
return handleInitialize(action, next); | ||
default: | ||
return next(action); | ||
} | ||
}; | ||
|
||
/** | ||
* This middleware ensure redux-form does not destroy forms after they are | ||
* remounted. This happens in a List component with a child (Datagrid, Tree) | ||
* containing forms as the List component clone them at each render, triggering a | ||
* unmount/mount. Related to this redux-form bug: | ||
* https://github.com/erikras/redux-form/issues/3435#issuecomment-359371803 | ||
*/ | ||
export default () => next => action => handleAction(action, next); |