Swagger UI for React
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.
Common errors
-
Module not found: Error: Can't resolve 'swagger-ui-react/swagger-ui.css'
cause The CSS file for Swagger UI styling is not imported or the import path is incorrect.fixAdd `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.
cause The `SwaggerUI` component was likely imported incorrectly, often as a CommonJS `require()` or as a named import when it's a default export.fixEnsure 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@...
cause `react` or `react-dom` peer dependencies are not met or are not installed in your project.fixInstall `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`).
cause 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.fixEnsure 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.
Warnings
- breaking Major 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.
- gotcha The `spec` and `url` props are mutually exclusive. Providing both can lead to unpredictable behavior in how the OpenAPI document is loaded and displayed.
- gotcha Certain 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.
- gotcha This package uses Scarf for anonymized installation analytics. This only occurs during `npm install` and helps support maintainers.
- breaking Recent 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.
Install
-
npm install swagger-ui-react -
yarn add swagger-ui-react -
pnpm add swagger-ui-react
Imports
- SwaggerUI
const SwaggerUI = require("swagger-ui-react");import SwaggerUI from "swagger-ui-react";
- CSS
import "swagger-ui-react/dist/swagger-ui.css";
import "swagger-ui-react/swagger-ui.css";
- Props (TypeScript)
import type { SwaggerUIProps } from "swagger-ui-react";
Quickstart
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;