undux
Version:
Dead simple state management for React
41 lines • 1.78 kB
JavaScript
import * as React from 'react';
import { equals, getDisplayName, keys, mapValues, some } from '../utils';
/**
* @deprecated Use `createConnectedStoreAs` instead.
*/
export function connectAs(stores) {
return function (Component) {
var _a;
return _a = class extends React.Component {
constructor() {
super(...arguments);
this.state = {
stores: mapValues(stores, (_) => _.getCurrentSnapshot()),
subscriptions: keys(stores).map((k) => stores[k].onAll().subscribe(({ previousValue, value }) => {
if (equals(previousValue, value)) {
return false;
}
this.setState((state) => ({
stores: Object.assign({}, state.stores, {
[k]: stores[k].getCurrentSnapshot(),
}),
}));
})),
};
}
componentWillUnmount() {
this.state.subscriptions.forEach((_) => _.unsubscribe());
}
shouldComponentUpdate(props, state) {
return (some(state.stores, (s, k) => s !== this.state.stores[k]) ||
Object.keys(props).some((_) => props[_] !== this.props[_]));
}
render() {
return React.createElement(Component, { ...this.props, ...this.state.stores });
}
},
_a.displayName = `withStore(${getDisplayName(Component)})`,
_a;
};
}
//# sourceMappingURL=connectAs.js.map