Skip to content

Commit

Permalink
useRef for expressionToRender
Browse files Browse the repository at this point in the history
  • Loading branch information
drewdaemon committed Mar 1, 2022
1 parent b540de4 commit 8a850f0
Showing 1 changed file with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React, { useState, useEffect, useMemo, useContext, useCallback } from 'react';
import React, { useState, useEffect, useMemo, useContext, useCallback, useRef } from 'react';
import classNames from 'classnames';
import { FormattedMessage } from '@kbn/i18n-react';
import { toExpression } from '@kbn/interpreter';
Expand Down Expand Up @@ -122,8 +122,6 @@ export const WorkspacePanel = React.memo(function WorkspacePanel(props: Workspac
);
});

let expressionToRender: null | undefined | string;

// Exported for testing purposes only.
export const InnerWorkspacePanel = React.memo(function InnerWorkspacePanel({
framePublicAPI,
Expand Down Expand Up @@ -151,11 +149,7 @@ export const InnerWorkspacePanel = React.memo(function InnerWorkspacePanel({
initialRenderComplete: false,
});

useEffect(() => {
return () => {
expressionToRender = null;
};
}, []);
const expressionToRender = useRef<null | undefined | string>();

const shouldApplyExpression =
autoApplyEnabled || !localState.initialRenderComplete || triggerApply;
Expand Down Expand Up @@ -262,16 +256,19 @@ export const InnerWorkspacePanel = React.memo(function InnerWorkspacePanel({
}, [_expression, dispatchLens]);

if (shouldApplyExpression) {
expressionToRender = _expression;
expressionToRender.current = _expression;
}

if (!autoApplyEnabled) {
dispatchLens(setChangesApplied(_expression === expressionToRender));
dispatchLens(setChangesApplied(_expression === expressionToRender.current));
}

const expressionExists = Boolean(expressionToRender);
const expressionExists = Boolean(expressionToRender.current);
// null signals an empty workspace which should count as an initial render
if ((expressionExists || expressionToRender === null) && !localState.initialRenderComplete) {
if (
(expressionExists || expressionToRender.current === null) &&
!localState.initialRenderComplete
) {
setLocalState((s) => ({ ...s, initialRenderComplete: true }));
}

Expand Down Expand Up @@ -375,12 +372,12 @@ export const InnerWorkspacePanel = React.memo(function InnerWorkspacePanel({
};

const renderVisualization = () => {
if (expressionToRender === null) {
if (expressionToRender.current === null) {
return renderEmptyWorkspace();
}
return (
<VisualizationWrapper
expression={expressionToRender}
expression={expressionToRender.current}
framePublicAPI={framePublicAPI}
lensInspector={lensInspector}
onEvent={onEvent}
Expand Down

0 comments on commit 8a850f0

Please sign in to comment.