Persist some component state on route changes.
parent
025bb57b35
commit
3cfdb07b6a
@ -0,0 +1,31 @@
|
||||
import * as React from 'react';
|
||||
import { withRouter } from 'react-router';
|
||||
|
||||
// FIXME: Should probably tie this to the history object, or at least based on the depth of the history
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
if (super.componentWillUnmount) {
|
||||
super.componentWillUnmount();
|
||||
}
|
||||
stateCache[this.getKeyForTag(this.props, tag)] = this.state;
|
||||
}
|
||||
|
||||
getKeyForTag(props: any, tagName: string) {
|
||||
return props.location.pathname + ':' + tagName;
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue