question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Event handlers are not getting called when using React.StrictMode on React 18.0.2

See original GitHub issue

Packages used:

powerbi-client: 2.21.1 powerbi-client-react: 1.3.5 react: 18.2.0 react-dom: 18.2.0

Problem description

After updating from React 17 to React 18, I noticed that event handlers were not getting called. I proceeded to remove <React.StrictMode>, and everything worked as expected.

Steps to reproduce

      <PowerBIEmbed
        embedConfig={{
          type: "report",
          tokenType: TokenType.Aad,
          settings: {
            personalBookmarksEnabled: true,
            background: BackgroundType.Transparent,
            panes: {
              filters: {
                visible: false,
              },
              pageNavigation: {
                visible: false,
              },
            },
          },
          accessToken: "<token>",
          embedUrl: "<embedUrl>",
        }}
        eventHandlers={new Map([
          ["loaded", () => {console.log("Report loaded")}],
        ])
      }
      />

Expected result: “Report Loaded” get’s printed in the console. Actual result: Report renders correctly, but nothing is logged in the console.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

1reaction
unowizcommented, Dec 9, 2022

I came across the same issue. Upgraded to React 18 and event handlers didn’t fire at all. I should have googled before. thanks @BrianUribe6 for logging this.

0reactions
ericwilhitecommented, Dec 14, 2022

I just ran into the same issue.

I’ve been playing with the component and found this solution that seems to work with React v18 and StrictMode.

It grabs the embedded component instance and uses the “.on” method for the component to add the handler. I also did the cleanup manually with the “.off” in the return of the useEffect as I’m not sure if it automatically cleans up the handlers.

import { PowerBIEmbed } from "powerbi-client-react";
import { models, Report } from "powerbi-client";

export const PowerBIReport = () => {

  const [embeddedReport, setEmbeddedReport] = useState<Report>();

  useEffect(() => {
    embeddedReport?.on("loaded", (event) => {
      console.log("loaded");
    });

    embeddedReport?.on("saved", (event) => {
      console.log("saved", event);
    });
    return () => {
      embeddedReport?.off("loaded");
      embeddedReport?.off("saved");
    };
  }, [embeddedReport]);

  return (
    <div className="h-screen">
      <PowerBIEmbed
        embedConfig={{
          type: "report",
          id: "",
          embedUrl: "",
          accessToken: "",
          tokenType: models.TokenType.Embed,
          permissions: models.Permissions.All,
          viewMode: models.ViewMode.Edit,
          settings: {
            bars: {
              actionBar: { visible: true },
            },
            panes: {
              filters: { expanded: false, visible: true },
            },
            background: models.BackgroundType.Default,
          },
        }}

        getEmbeddedComponent={(r) => {
          setEmbeddedReport(r as Report);
        }}
      />
    </div>
  );
};

Read more comments on GitHub >

github_iconTop Results From Across the Web

Strict Mode - React
StrictMode is a tool for highlighting potential problems in an application. Like Fragment , StrictMode does not render any visible UI.
Read more >
javascript - React.StrictMode seems to work differently in ...
I got a solution here, where it's saying to remove the React.StrictMode from index.js and after doing so it's working fine. Then I...
Read more >
React 18 New Features – Concurrent Rendering, Automatic ...
Strict mode will ensure components are resilient to effects being mounted and unmounted multiple times.
Read more >
React 18 Upgrade Guide and New Features - Refine Dev
By executing the following commands in a terminal of your working directory, you can upgrade or install React 18 using NPM or Yarn...
Read more >
What's New in React 18 - AppSignal Blog
To ensure you migrate your app properly, try enabling strict mode. Strict mode will let you know what is happening with components in ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found