google-map-react/develop/utils/withStateSelector.js
Michael Diego c35f73569c
Update dependencies and fix develop env (#897)
* Upgrade packages
* Update yarn.lock
* Fix warnings
* Fix sass dependencies
* Fix sass modules
2020-07-24 01:54:20 -03:00

46 lines
1.3 KiB
JavaScript

import { Component } from 'react';
import createHelper from './createHelper';
import createEagerFactory from './createEagerFactory';
const withStateSelector = (stateName, stateUpdaterName, selectorFactory) =>
BaseComponent => {
const factory = createEagerFactory(BaseComponent);
return class extends Component {
selector = selectorFactory();
state = {
stateValue: this.selector(this.props),
};
updateStateValue = (updateFn, callback) =>
this.setState(
({ stateValue }) => ({
stateValue: typeof updateFn === 'function'
? updateFn(stateValue)
: updateFn,
}),
callback
);
// eslint-disable-next-line camelcase
UNSAFE_componentWillReceiveProps(nextProps) {
// reselect memoize result
const nextStateValue = this.selector(nextProps);
if (nextStateValue !== this.state.stateValue) {
this.setState({
stateValue: nextStateValue,
});
}
}
render() {
return factory({
...this.props,
[stateName]: this.state.stateValue,
[stateUpdaterName]: this.updateStateValue,
});
}
};
};
export default createHelper(withStateSelector, 'withStateSelector');