React Plotly.js Component
react-plotly.js is the official React component wrapper for the plotly.js graphing library, currently at stable version 2.6.0. It provides a declarative way to render interactive charts within React applications. The library maintains an active release cadence, with minor updates addressing bug fixes and new plotly.js event support occurring every few months. A key differentiator is its design as a 'dumb' React component, meaning it does not internally manage plot interactions (like zooming or panning) as part of its state. Developers must explicitly capture and manage these changes using callback props like `onUpdate` or `onInitialized` to persist user interactions across re-renders. It requires `react` and `plotly.js` as peer dependencies, allowing users to control the plotly.js bundle size and version and integrate custom bundles. This component forms the basis of Plotly's React component suite.
Common errors
-
Plot does not update or reflect user interactions after data changes.
cause The component is 'dumb' and expects immutable updates to props, or explicit revisioning for plot refreshes.fixPass new object references for `data`, `layout`, or `config` props when they change, or increment the `revision` prop or `layout.datarevision` to force an update. Capture interactive changes via `onUpdate`. -
TypeError: Cannot read properties of undefined (reading 'default') or 'react_plotly_js__WEBPACK_IMPORTED_MODULE_2__.Plot is not a constructor'
cause Incorrect import statement for the `Plot` component, especially when mixing CommonJS `require` with a default ESM export.fixUse `import Plot from 'react-plotly.js';` for ES Modules. If using CommonJS, use `const Plot = require('react-plotly.js').default;`.
Warnings
- breaking Version 2.3.0 removed deprecated React methods. Applications relying on older React lifecycle methods might break upon upgrade.
- gotcha The component is 'dumb' and does not manage plot state internally. User interactions like zooming or panning are lost on re-render unless explicitly captured.
- gotcha Plot updates require immutable data changes or explicit revisioning. Modifying `data` or `layout` properties in place (mutably) will not trigger a plot refresh.
- breaking The component may mutate its `layout` and `data` props internally in response to user interactions or plot updates. This is an anti-pattern in React and can lead to unexpected side effects if the original prop objects are expected to remain immutable.
Install
-
npm install react-plotly.js -
yarn add react-plotly.js -
pnpm add react-plotly.js
Imports
- Plot
import { Plot } from 'react-plotly.js'; const Plot = require('react-plotly.js');import Plot from 'react-plotly.js';
Quickstart
import React from 'react';
import Plot from 'react-plotly.js';
class App extends React.Component {
render() {
return (
<Plot
data={[
{
x: [1, 2, 3],
y: [2, 6, 3],
type: 'scatter',
mode: 'lines+markers',
marker: {color: 'red'},
},
{type: 'bar', x: [1, 2, 3], y: [2, 5, 3]},
]}
layout={{width: 720, height: 480, title: 'A Fancy Plot'}}
config={{displayModeBar: true, responsive: true}}
/>
);
}
}
export default App;