Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use custom UI when highcharts throw error #498

Open
ranafaraznaru opened this issue Nov 20, 2024 · 2 comments
Open

How to use custom UI when highcharts throw error #498

ranafaraznaru opened this issue Nov 20, 2024 · 2 comments
Assignees

Comments

@ranafaraznaru
Copy link

ranafaraznaru commented Nov 20, 2024

After extensive research, I've decided to post my question here.

I'm working with React Highcharts and I'm trying to implement custom error handling. Instead of the entire page crashing when Highcharts encounters an error, I want to display a custom UI component.

I've looked through Stack Overflow and the official Highcharts documentation, but I haven't been able to find a solution that works for me.

I've provided a CodeSandbox link below. Please help me achieve this, thank you!
https://codesandbox.io/p/sandbox/highcharts-react-forked-hfc6y3?workspaceId=b878446b-0701-41fb-b944-d231bbae84f1

Code :

import React, { useState, useEffect } from "react";
import { render } from "react-dom";
import HighchartsReact from "highcharts-react-official";
import Highcharts from "highcharts";

const App = () => {
  const [hasError, setHasError] = useState(false);

  const [options] = useState({
    chart: {
      type: "sdcnsdcns",
    },
    series: [
      {
        data: [4, 3, 5, 6, 2, 3],
      },
    ],
  });
  useEffect(() => {
    const handleError = (err) => {
      console.error("Highcharts Error:", err);
      setHasError(true); // Trigger React re-render
    };

    // Attach the Highcharts error event
    Highcharts.addEvent(Highcharts, "displayError", handleError);

    // return () => {
    //   // Clean up the error event listener
    //   Highcharts.removeEvent(Highcharts, "displayError", handleError);
    // };
  }, []);

  if (hasError) {
    // Render custom error UI
    return (
      <div className="error-ui">
        <h2>Error Loading Chart</h2>
        <p>An error occurred. Please refresh or try again later.</p>
      </div>
    );
  }
  return (
    <div>
      <HighchartsReact highcharts={Highcharts} options={options} />
    </div>
  );
};

render(<App />, document.getElementById("root"));
@ranafaraznaru ranafaraznaru reopened this Nov 20, 2024
@bm64 bm64 transferred this issue from highcharts/highcharts Nov 21, 2024
@KamilKubik KamilKubik self-assigned this Nov 22, 2024
@KamilKubik
Copy link
Contributor

Hi ranafaraznaru!

Highcharts throws a default Error object that should be handled during the runtime on the React side to display the fallback UI. What React recommends is the green note under this section. Considering this, I think the easiest way would be to wrap the Highcharts component with the custom error boundary. Kindly refer to an example based on the mentioned React docs, demo.

The above example shows the properly rendered UI, but the message persists (you can close it and see the UI) because React uses the react-error-overlay package in the development mode when initializing it with create-react-app (so depending on your project, you may see it or not).

To sump up, I'd highly recommend using the official React boundary (or the react-error-boundary package they recommend) to handle the UI and test it in the React production mode, not to experience the error.

Kind Regards!

@ranafaraznaru
Copy link
Author

Hi @KamilKubik Thanks i will test and come back here again if i need anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants