Skip to content

Instantly share code, notes, and snippets.

@dtothefp
Created March 7, 2017 18:41
Show Gist options
  • Save dtothefp/c04ddba8ec6110e5f59f25045a67b464 to your computer and use it in GitHub Desktop.
Save dtothefp/c04ddba8ec6110e5f59f25045a67b464 to your computer and use it in GitHub Desktop.
import React, {createClass} from 'react';
import {assign} from 'lodash';
import hoistNonReactStatics from 'hoist-non-react-statics';
function createComponent(Component, dataBindings) {
const displayName = `ProvideContextComponent(${Component.displayName || Component.name})`;
const ProvideContextComponent = createClass({
displayName,
contextTypes: dataBindings,
render() {
/**
* Important to override context values with props because `formId` can be passed on the
* `<Form />` as props or as context
*/
return React.createElement(Component, assign({}, this.state, this.context, this.props));
}
});
hoistNonReactStatics(ProvideContextComponent, Component);
return ProvideContextComponent;
}
/*
* Decorator using `React.Component`
*/
export default function provideContextDec(Component, dataBindings) {
if (arguments.length === 0 || typeof arguments[0] !== 'function') {
dataBindings = arguments[0];
return function connectToData(ComponentToDecorate) {
return createComponent(ComponentToDecorate, dataBindings);
};
}
return createComponent.apply(null, arguments);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment