Skip to content

Instantly share code, notes, and snippets.

@haradakunihiko
Last active October 16, 2015 15:22
Show Gist options
  • Save haradakunihiko/4d27dadd4aba951f6582 to your computer and use it in GitHub Desktop.
Save haradakunihiko/4d27dadd4aba951f6582 to your computer and use it in GitHub Desktop.
import { connect } from 'react-redux';
import { createSelectorCreator } from 'reselect';
import { reduxForm } from 'redux-form';
function memoize(func, equalityCheck = (a, b) => a === b) {
const lastArgsCache = {};
const lastResultCache = {};
return (...args) => {
const memoizeKey = args.pop();
const lastArgs = lastArgsCache[memoizeKey];
const lastResult = lastResultCache[memoizeKey];
if (lastArgs !== undefined &&
args.every((value, index) => equalityCheck(value, lastArgs[index]))) {
return lastResult;
}
const result = func(...args);
lastArgsCache[memoizeKey] = args;
lastResultCache[memoizeKey] = result;
return result;
};
}
function createSelector(...args) {
return createSelectorCreator(memoize)(...args);
}
export function connectReduxForm(...args) {
const formName = args[0].form;
const selector = createSelector(
({ form }, { formKey }) => form && form[formName] && (formKey ? form[formName][formKey] : form[formName]),
( state, { formKey } ) => formKey,
( state, { formKey } ) => formKey || '_default_memoize_key',
( form, formKey ) => formKey ? { form: { [formName]: { [formKey]: form } } } : { form: { [formName]: form } }
);
return DecoratedComponent => connect(selector)(reduxForm(...args)(DecoratedComponent));
}
@haradakunihiko
Copy link
Author

Improve performance issue of multiple records of form with redux-form.
Especially when there are more than 30-40 records, focusing one single form element forces all redux-form-connected component to rerender. It's possible to implement shouldComponentUpdate function, but still there is a waste of processing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment