Files
grafana/public/app/plugins/panel/geomap/components/ObservablePropsWrapper.tsx
kay delaney 4c7732ad17 Chore: Upgrade Typescript to 4.8.2 (#54901)
* Chore: Upgrade typescript to 4.8.2
2022-09-09 16:17:58 +01:00

45 lines
873 B
TypeScript

import React, { Component } from 'react';
import { Observable, Unsubscribable } from 'rxjs';
interface Props<T> {
watch: Observable<T>;
child: React.ComponentType<T>;
initialSubProps: T;
}
interface State<T> {
subProps: T;
}
export class ObservablePropsWrapper<T extends {}> extends Component<Props<T>, State<T>> {
sub?: Unsubscribable;
constructor(props: Props<T>) {
super(props);
this.state = {
subProps: props.initialSubProps,
};
}
componentDidMount() {
this.sub = this.props.watch.subscribe({
next: (subProps: T) => {
this.setState({ subProps });
},
complete: () => {},
error: (err) => {},
});
}
componentWillUnmount() {
if (this.sub) {
this.sub.unsubscribe();
}
}
render() {
const { subProps } = this.state;
return <this.props.child {...subProps} />;
}
}