Registry / web-framework / swagger-ui-react

swagger-ui-react

JSON →
library5.32.4jsnpmunverified

swagger-ui-react is a specialized React component that seamlessly integrates Swagger UI directly into React applications. It exposes the full capabilities of Swagger UI through a simple React component, `SwaggerUI`. Unlike the core Swagger UI distribution, this package declares `react` and `react-dom` (versions >=16.8.0 <20) as peer dependencies, ensuring compatibility with existing React environments. The package's versioning closely mirrors the underlying Swagger UI, with the current stable release being 5.32.4. It receives frequent updates, including bug fixes, performance enhancements, and crucial security patches, as demonstrated by recent releases addressing several CVEs and introducing features like basic OpenAPI 3.2.0 support and dark mode. A notable aspect is the inclusion of anonymized installation analytics via Scarf, which users can easily opt out of through `package.json` settings or an environment variable. Developers should be mindful of specific prop behaviors, such as `layout` and `docExpansion` applying only on initial mount, and the mutual exclusivity of the `spec` and `url` props.

npm install swagger-ui-react
INSTALL
IMPORT
SIG · SWAGGER-UI-REACT
S
swagger-ui-react
web-frameworkjavascriptv5.32.4
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SwaggerUI
import SwaggerUI from "swagger-ui-react";
const SwaggerUI = require("swagger-ui-react");
Primary component for rendering Swagger UI. This is an ESM default export.
CSS
import "swagger-ui-react/swagger-ui.css";
import "swagger-ui-react/dist/swagger-ui.css";
Required to style the Swagger UI component. The correct path is directly under the package root.
Props (TypeScript)
import type { SwaggerUIProps } from "swagger-ui-react";
Use `import type` for type-only imports in TypeScript environments.

This quickstart demonstrates how to integrate `SwaggerUI` into a React component, loading an OpenAPI specification from a URL, applying initial documentation expansion settings, and including basic interceptor and completion callbacks. It also shows a common pattern for defining the API URL via environment variables.

import React from "react"; import SwaggerUI from "swagger-ui-react"; import "swagger-ui-react/swagger-ui.css"; const App = () => { const apiUrl = process.env.REACT_APP_SWAGGER_URL || "https://petstore.swagger.io/v2/swagger.json"; const initialDocExpansion = "list"; // 'list', 'full', 'none' return ( <div style={{ padding: '20px' }}> <h1>My API Documentation</h1> <SwaggerUI url={apiUrl} docExpansion={initialDocExpansion} requestInterceptor={(req) => { // Example: Add an authorization header // req.headers.Authorization = `Bearer ${someAuthToken}`; return req; }} onComplete={(system) => { console.log("Swagger UI finished rendering.", system); }} /> </div> ); }; export default App;
Debug
Known issues
breakingMajor versions of `swagger-ui-react` correspond to `swagger-ui` releases. Upgrading major versions may introduce breaking changes in API behavior, configuration options, or UI rendering. Always review the `swagger-ui` changelog when updating major versions.
fix
Consult the official Swagger UI release notes for breaking changes before upgrading. Test thoroughly in a staging environment.
affects: >=1.0.0
gotchaThe `spec` and `url` props are mutually exclusive. Providing both can lead to unpredictable behavior in how the OpenAPI document is loaded and displayed.
fix
Always use either `spec` (for inline JSON/YAML) OR `url` (for remote fetching), but never both simultaneously.
affects: >=1.0.0
gotchaCertain props, such as `layout` and `docExpansion`, are currently only applied once on initial mount of the component. Subsequent changes to these props will not cause the underlying Swagger UI instance to update.
fix
If dynamic changes to these specific props are required, consider remounting the `SwaggerUI` component (e.g., by changing its `key` prop) or explore using the Swagger UI system object obtained via `onComplete` for programmatic control.
affects: >=1.0.0
gotchaThis package uses Scarf for anonymized installation analytics. This only occurs during `npm install` and helps support maintainers.
fix
To opt out, set `"scarfSettings": { "enabled": false }` in your `package.json` or set the environment variable `SCARF_ANALYTICS=false` during installation.
affects: >=5.x
breakingRecent versions (e.g., v5.32.4, v5.32.2) include fixes for high-severity CVEs in underlying Docker images and dependencies like `libpng` and `zlib`. While these are primarily for Docker users, dependency updates in patch releases can sometimes introduce subtle behavioral changes.
fix
Ensure you are on the latest patch release to benefit from security updates. Review the changelog for specific dependency bumps.
affects: >=5.30.0
Errors
Common errors & fixes
Module not found: Error: Can't resolve 'swagger-ui-react/swagger-ui.css'
The CSS file for Swagger UI styling is not imported or the import path is incorrect.
fix
Add `import "swagger-ui-react/swagger-ui.css";` to your component or main application file to ensure styles are loaded.
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
The `SwaggerUI` component was likely imported incorrectly, often as a CommonJS `require()` or as a named import when it's a default export.
fix
Ensure you are using `import SwaggerUI from "swagger-ui-react";` for the default export.
npm ERR! ERESOLVE unable to resolve dependency tree ... peer react@"..." from swagger-ui-react@...
`react` or `react-dom` peer dependencies are not met or are not installed in your project.
fix
Install `react` and `react-dom` in your project with versions compatible with `swagger-ui-react` (e.g., `npm install react react-dom`). The current peer dependency range is `>=16.8.0 <20`.
Warning: Cannot update a component (`SwaggerUI`) while rendering a different component (`App`).
Attempting to update a Swagger UI prop (e.g., `url` or `spec`) based on state changes within the same render cycle can lead to React warnings.
fix
Ensure that props passed to `SwaggerUI` are stable during a render. If `url` or `spec` need to change, ensure the change is triggered by a user interaction or a state update outside of the direct render function flow, or consider using `React.memo` or `useMemo` for complex prop values.
Upgrade
Version history
5.32.4latest on npm
Audit
Dependencies
reactrequiredRequired peer dependency for the React component to function.
react-domrequiredRequired peer dependency for rendering the React component.
Agent activity
4 hits · last 30 days
node
4
Resources