Power BI React Component
This library provides a React wrapper for the underlying `powerbi-client` JavaScript library, enabling seamless integration of various Power BI artifacts—reports, dashboards, tiles, visuals, Q&A, and paginated reports—directly within React applications. It also facilitates the creation of new Power BI reports within the application context. The current stable version is 2.0.2, with recent major updates like v2.0.0 upgrading the peer dependency to React 18, which introduced breaking changes for older React environments. Release cadence is driven by feature additions, bug fixes, and compatibility updates, without a strict schedule but maintaining active development. Its key differentiators include a declarative, component-based API that simplifies embedding compared to direct `powerbi-client` usage, built-in lifecycle management, and support for advanced features like report bootstrapping for performance optimization and dynamic configuration updates.
Common errors
-
Error: You are running React 17. PowerBI-client-react v2 requires React 18 or higher.
cause Mismatch between application's React version and `powerbi-client-react` peer dependency.fixUpgrade React and ReactDOM to version 18.x in your project (`npm install react@^18 react-dom@^18` or `yarn add react@^18 react-dom@^18`). -
ReferenceError: models is not defined
cause `models` object from `powerbi-client` was not imported.fixAdd `import * as models from 'powerbi-client';` to your component file. -
TypeError: Cannot read properties of undefined (reading 'detail')
cause An event handler tried to access `event.detail` but the `event` object received did not have that property or was `undefined`.fixEnsure your event handler function correctly inspects the `event` object's structure as per `powerbi-client` documentation. Not all events might have a `detail` property, or it might be null. -
Power BI embed error: A token or embed URL is missing.
cause The `embedConfig` prop is missing either `accessToken`, `embedUrl`, or `id` required for embedding.fixVerify that `embedConfig.accessToken`, `embedConfig.embedUrl`, and `embedConfig.id` are correctly populated with valid, non-empty string values from your Power BI embedding service.
Warnings
- breaking Version 2.0.0 and above now require React 18 as a peer dependency. Applications using older React versions will experience runtime errors or build failures.
- breaking In v2.0.0, the re-embed logic was fixed to ensure the component re-embeds correctly on every configuration change. This might alter existing behavior for applications that relied on previous, potentially incorrect, re-embedding patterns.
- gotcha The `models` object, crucial for `TokenType` and other configurations, is part of the `powerbi-client` library, not re-exported by `powerbi-client-react`. Forgetting to import it leads to runtime errors.
- gotcha When bootstrapping a Power BI entity, the `embedConfig` must initially have `accessToken: undefined` (or null/empty string). To actually embed the report after bootstrap, you *must* update the `embedConfig` props with a valid `accessToken` and other details.
- gotcha Event handlers are passed as a `Map` object, not a plain JavaScript object. Incorrectly using a plain object will result in event handlers not being registered.
Install
-
npm install powerbi-client-react -
yarn add powerbi-client-react -
pnpm add powerbi-client-react
Imports
- PowerBIEmbed
const PowerBIEmbed = require('powerbi-client-react').PowerBIEmbed;import { PowerBIEmbed } from 'powerbi-client-react'; - models
import { models } from 'powerbi-client-react';import * as models from 'powerbi-client';
- Report
import { Report } from 'powerbi-client-react';import { Report } from 'powerbi-client';
Quickstart
import { PowerBIEmbed } from 'powerbi-client-react';
import * as models from 'powerbi-client';
function MyPowerBIReport() {
const reportId = process.env.POWERBI_REPORT_ID ?? '';
const embedUrl = process.env.POWERBI_EMBED_URL ?? '';
const accessToken = process.env.POWERBI_ACCESS_TOKEN ?? '';
if (!reportId || !embedUrl || !accessToken) {
return <p>Please configure Power BI environment variables.</p>;
}
return (
<div style={{ height: '600px', width: '100%' }}>
<PowerBIEmbed
embedConfig={{
type: 'report',
id: reportId,
embedUrl: embedUrl,
accessToken: accessToken,
tokenType: models.TokenType.Embed,
settings: {
panes: {
filters: {
expanded: false,
visible: false
}
},
background: models.BackgroundType.Transparent
}
}}
eventHandlers={
new Map([
['loaded', () => console.log('Report loaded')],
['rendered', () => console.log('Report rendered')],
['error', (event) => console.error('Power BI error:', event.detail)],
['pageChanged', (event) => console.log('Page changed:', event.detail)]
])
}
cssClassName="my-powerbi-report"
getEmbeddedComponent={(embeddedReport) => {
console.log('Embedded component reference:', embeddedReport);
// You can store this reference to interact with the report later
}}
/>
</div>
);
}
export default MyPowerBIReport;