Git Product home page Git Product logo

Comments (2)

dosubot avatar dosubot commented on September 13, 2024 2

Hey @ohitsdoh! 👋 I'm here to help you with bug fixes, answer questions, and guide you on becoming a contributor. Let's squash those bugs together!

To resolve the "Maximum update depth exceeded" error in Apache Superset when loading a dashboard with many filters, you can optimize the useEffect hooks to depend on more granular information instead of big objects that require deep comparison. This can help prevent unnecessary re-renders and infinite loops.

Here's a revised version of the useEffect hook with more granular dependencies:

useEffect(() => {
  if (!inViewFirstTime) {
    return;
  }

  const newFormData = getFormData({
    ...filter,
    datasetId,
    dependencies,
    groupby,
    adhoc_filters,
    time_range,
    dashboardId,
  });

  const filterOwnState = filter.dataMask?.ownState || {};

  const customizer = (
    objValue: Partial<QueryFormData>,
    othValue: Partial<QueryFormData>,
    key: string,
  ) => (key === 'url_params' ? true : undefined);

  const formDataChanged = !isEqualWith(formData, newFormData, customizer);
  const ownStateChanged = !isEqual(ownState, filterOwnState);

  if (!isRefreshing && (formDataChanged || ownStateChanged || shouldRefresh)) {
    setFormData(newFormData);
    setOwnState(filterOwnState);

    if (!hasDataSource) {
      return;
    }

    setIsRefreshing(true);

    getChartDataRequest({
      formData: newFormData,
      force: shouldRefresh,
      ownState: filterOwnState,
    })
      .then(({ response, json }) => {
        if (isFeatureEnabled(FeatureFlag.GlobalAsyncQueries)) {
          const result = 'result' in json ? json.result[0] : json;

          if (response.status === 200) {
            setState([result]);
            handleFilterLoadFinish();
          } else if (response.status === 202) {
            waitForAsyncData(result)
              .then((asyncResult: ChartDataResponseResult[]) => {
                setState(asyncResult);
                handleFilterLoadFinish();
              })
              .catch((error: Response) => {
                getClientErrorObject(error).then(clientErrorObject => {
                  setError(clientErrorObject);
                  handleFilterLoadFinish();
                });
              });
          } else {
            throw new Error(
              `Received unexpected response status (${response.status}) while fetching chart data`,
            );
          }
        } else {
          setState(json.result);
          setError(undefined);
          handleFilterLoadFinish();
        }
      })
      .catch((error: Response) => {
        getClientErrorObject(error).then(clientErrorObject => {
          setError(clientErrorObject);
          handleFilterLoadFinish();
        });
      });
  }
}, [
  inViewFirstTime,
  dependencies.length, // Use length instead of the entire array
  datasetId,
  groupby,
  adhoc_filters.length, // Use length instead of the entire array
  time_range,
  dashboardId,
  handleFilterLoadFinish,
  hasDataSource,
  isRefreshing,
  shouldRefresh,
]);

By using more specific dependencies like dependencies.length and adhoc_filters.length, you can reduce the likelihood of unnecessary re-renders and avoid the "Maximum update depth exceeded" error [1].

Additionally, ensure that the applyCharts method in the Dashboard component is optimized to avoid unnecessary updates. The applyCharts method should only trigger updates when there are actual changes in the filters or charts. This can be achieved by comparing the current and previous states of filters and charts using utility functions like areObjectsEqual.

applyCharts() {
  const { hasUnsavedChanges, editMode } = this.props.dashboardState;

  const { appliedFilters, appliedOwnDataCharts } = this;
  const { activeFilters, ownDataCharts, chartConfiguration } = this.props;
  if (
    isFeatureEnabled(FeatureFlag.DashboardCrossFilters) &&
    !chartConfiguration
  ) {
    // For a first loading we need to wait for cross filters charts data loaded to get all active filters
    // for correct comparing of filters to avoid unnecessary requests
    return;
  }

  if (
    !editMode &&
    (!areObjectsEqual(appliedOwnDataCharts, ownDataCharts, {
      ignoreUndefined: true,
    }) ||
      !areObjectsEqual(appliedFilters, activeFilters, {
        ignoreUndefined: true,
      }))
  ) {
    this.applyFilters();
  }

  if (hasUnsavedChanges) {
    Dashboard.onBeforeUnload(true);
  } else {
    Dashboard.onBeforeUnload(false);
  }
}

By ensuring that applyCharts only triggers updates when necessary, you can further reduce the risk of encountering the "Maximum update depth exceeded" error [2].

To continue talking to Dosu, mention @dosu.

from superset.

rusackas avatar rusackas commented on September 13, 2024

How many is a lot? Can you reproduce this somehow with example dashboards / datasets?

from superset.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.