mirror of
https://github.com/google-map-react/google-map-react.git
synced 2025-12-08 18:26:32 +00:00
* Create our own createHelper, its not in recompose anymore * Use our withStateSelector * Proper apiKey usage * Replace apiKey with bootstrapUrlKeys * No need of true value in html * Use lodash.omit instead in dev * Remove unused file * Remove unnecessary extra folder utils * Upgrade recompose again * Oops! Move lodash.omit to devDependencies * Fix webpack files styles * Make examples bigger in width
45 lines
1.2 KiB
JavaScript
45 lines
1.2 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
|
|
);
|
|
|
|
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');
|