history persistor: follow the recommended HOC guidelines.
parent
5a1b3af9b0
commit
331d85d3a2
|
@ -36,7 +36,7 @@ class JournalCalendar extends React.PureComponent {
|
|||
}
|
||||
|
||||
render() {
|
||||
const PersistCalendar = historyPersistor(Calendar, 'Calendar');
|
||||
const PersistCalendar = historyPersistor('Calendar')(Calendar);
|
||||
let items = this.props.entries;
|
||||
|
||||
return (
|
||||
|
|
|
@ -77,7 +77,7 @@ class PimMain extends React.PureComponent {
|
|||
} as any,
|
||||
};
|
||||
|
||||
const PersistCalendar = historyPersistor(Calendar, 'Calendar');
|
||||
const PersistCalendar = historyPersistor('Calendar')(Calendar);
|
||||
|
||||
return (
|
||||
<React.Fragment>
|
||||
|
@ -114,4 +114,4 @@ class PimMain extends React.PureComponent {
|
|||
}
|
||||
}
|
||||
|
||||
export default historyPersistor(PimMain, 'PimMain');
|
||||
export default historyPersistor('PimMain')(PimMain);
|
||||
|
|
|
@ -6,26 +6,28 @@ let stateCache = {};
|
|||
|
||||
type Constructor<T> = new(...args: any[]) => T;
|
||||
|
||||
export function historyPersistor<T extends Constructor<React.Component>>(Base: T, tag: string) {
|
||||
return withRouter(class extends Base {
|
||||
constructor(...rest: any[]) {
|
||||
const props = rest[0];
|
||||
super(...rest);
|
||||
const tagName = this.getKeyForTag(props, tag);
|
||||
if (tagName in stateCache) {
|
||||
this.state = stateCache[tagName];
|
||||
export function historyPersistor(tag: string) {
|
||||
return function<T extends Constructor<React.Component>>(Base: T) {
|
||||
return withRouter(class extends Base {
|
||||
constructor(...rest: any[]) {
|
||||
const props = rest[0];
|
||||
super(...rest);
|
||||
const tagName = this.getKeyForTag(props, tag);
|
||||
if (tagName in stateCache) {
|
||||
this.state = stateCache[tagName];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
if (super.componentWillUnmount) {
|
||||
super.componentWillUnmount();
|
||||
componentWillUnmount() {
|
||||
if (super.componentWillUnmount) {
|
||||
super.componentWillUnmount();
|
||||
}
|
||||
stateCache[this.getKeyForTag(this.props, tag)] = this.state;
|
||||
}
|
||||
stateCache[this.getKeyForTag(this.props, tag)] = this.state;
|
||||
}
|
||||
|
||||
getKeyForTag(props: any, tagName: string) {
|
||||
return props.location.pathname + ':' + tagName;
|
||||
}
|
||||
});
|
||||
getKeyForTag(props: any, tagName: string) {
|
||||
return props.location.pathname + ':' + tagName;
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue