React Gauge Component
react-gauge-component is a customizable React component designed for displaying gauge charts, often used for data visualization like speedometers or Grafana-style gauges. The current stable version is 2.0.28, with frequent patch and minor updates within the 2.x series. It was forked from `@Martin36/react-gauge-chart` and significantly re-architected in its v2 release to provide enhanced customization options and modern React compatibility. Key differentiators include robust TypeScript support, a flexible API for segment and value rendering, and an online sandbox editor for easy configuration. It requires React 16.8.2 or newer as a peer dependency, ensuring broad compatibility with contemporary React ecosystems.
Common errors
-
ReferenceError: document is not defined
cause Attempting to render `react-gauge-component` in a Server-Side Rendering (SSR) environment without disabling SSR for the component.fixFor Next.js, use `const GaugeComponent = dynamic(() => import('react-gauge-component'), { ssr: false });`. For other SSR frameworks, ensure the component is only loaded client-side. -
TypeError: Cannot read properties of undefined (reading 'current')
cause This error often indicates that a React ref is being accessed before it's attached to a DOM element, which can happen if the component tries to render too early or if there's a problem with the rendering lifecycle in an SSR context.fixThis is often related to SSR issues. Apply dynamic import with `ssr: false` as described in the warnings. Also, ensure React and React-DOM peer dependencies are correctly installed and compatible. -
Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
cause This common React error can occur if there are multiple versions of React loaded in your application, or if the rules of hooks are violated, sometimes indirectly due to incompatible peer dependencies.fixCheck for duplicate React installations using `npm ls react` or `yarn why react`. Ensure `react` and `react-dom` peer dependencies are met and are singular in your dependency tree. Re-evaluate any custom rendering logic around the gauge component.
Warnings
- breaking The package underwent a major refactor and API overhaul with the release of v2.0.20. Code written for v1.x is not directly compatible with v2.x and requires migration.
- gotcha When using `react-gauge-component` within Server-Side Rendering (SSR) frameworks like Next.js, importing the component directly will lead to 'document is not defined' errors because the component expects a browser DOM environment.
- gotcha Incorrect or incompatible React and React-DOM peer dependencies can lead to runtime errors or unexpected behavior. The component relies on specific React features.
Install
-
npm install react-gauge-component -
yarn add react-gauge-component -
pnpm add react-gauge-component
Imports
- GaugeComponent
const GaugeComponent = require('react-gauge-component');import GaugeComponent from 'react-gauge-component';
- GaugeComponent (Named)
import GaugeComponent from 'react-gauge-component';
import { GaugeComponent } from 'react-gauge-component'; - GaugeComponent (Next.js Dynamic Import)
import GaugeComponent from 'react-gauge-component';
import dynamic from 'next/dynamic'; const GaugeComponent = dynamic(() => import('react-gauge-component'), { ssr: false });
Quickstart
import React from 'react';
import GaugeComponent from 'react-gauge-component';
function MyGauge() {
return (
<div style={{ width: '300px', height: 'auto' }}>
<GaugeComponent
arc={{
width: 0.2,
padding: 0.005,
cornerRadius: 1,
subArcs: [
{ limit: 15, color: '#EA4228', showPoint: true },
{ limit: 35, color: '#F5CD19', showPoint: true },
{ limit: 65, color: '#5BE12C', showPoint: true },
{ limit: 100, color: '#444444', showPoint: true }
]
}}
value={75}
labels={{
valueLabel: {
formatTextValue: value => value + ' km/h'
}
}}
/>
</div>
);
}
export default MyGauge;