Skip to content

Commit

Permalink
Improve rendering in theme hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Roes committed Jul 30, 2021
1 parent a9f26fa commit 4156dcc
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/plugins/charts/public/services/theme/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import { useEffect, useState } from 'react';
import { useEffect, useRef, useState } from 'react';
import { Observable, BehaviorSubject } from 'rxjs';

import { CoreSetup } from 'kibana/public';
Expand Down Expand Up @@ -54,11 +54,18 @@ export class ThemeService {
/** A React hook for consuming the charts theme */
public useChartsTheme = (): PartialTheme => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const [value, update] = useState(this.chartsDefaultTheme);
const [value, update] = useState(this._chartsTheme$.getValue());
// eslint-disable-next-line react-hooks/rules-of-hooks
const ref = useRef(value);

// eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(() => {
const s = this.chartsTheme$.subscribe(update);
const s = this.chartsTheme$.subscribe((val) => {
if (val !== ref.current) {
ref.current = val;
update(val);
}
});
return () => s.unsubscribe();
}, []);

Expand All @@ -68,11 +75,18 @@ export class ThemeService {
/** A React hook for consuming the charts theme */
public useChartsBaseTheme = (): Theme => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const [value, update] = useState(this.chartsDefaultBaseTheme);
const [value, update] = useState(this._chartsBaseTheme$.getValue());
// eslint-disable-next-line react-hooks/rules-of-hooks
const ref = useRef(value);

// eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(() => {
const s = this.chartsBaseTheme$.subscribe(update);
const s = this.chartsBaseTheme$.subscribe((val) => {
if (val !== ref.current) {
ref.current = val;
update(val);
}
});
return () => s.unsubscribe();
}, []);

Expand Down

0 comments on commit 4156dcc

Please sign in to comment.