react-redux-isomorphic
Version:
Set of utilities for creating isomorphic applications using react-redux
38 lines (32 loc) • 1.41 kB
JavaScript
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import { connect } from 'react-redux';
import invariant from 'invariant';
import IsomorphicWrapper from './IsomorphicWrapper';
import * as actions from './actions';
import getComponentState from './getComponentState';
var isomorphic = function isomorphic(_ref) {
var isomorphicId = _ref.isomorphicId,
getContext = _ref.getContext,
shouldReload = _ref.shouldReload;
invariant(typeof getContext === 'function', 'getContext should be a function');
if (shouldReload) {
invariant(typeof shouldReload === 'function', 'shouldReload should be a function');
}
return function (component) {
var mapStateToProps = function mapStateToProps(storeState, componentProps) {
var currentIsomorphicId = isomorphicId || componentProps.isomorphicId;
invariant(currentIsomorphicId, 'isomorphicId is required in isomorphic decorated component props or decorator params');
return {
component: component,
componentProps: componentProps,
isomorphicId: currentIsomorphicId,
isomorphic: getComponentState(storeState, currentIsomorphicId),
getContext: getContext,
shouldReload: shouldReload
};
};
var mapDispatchToProps = _objectSpread({}, actions);
return connect(mapStateToProps, mapDispatchToProps)(IsomorphicWrapper);
};
};
export default isomorphic;