React File Icon Component
react-file-icon is a React component library designed to render scalable vector graphic (SVG) icons representing various file types. It provides a `FileIcon` component that can display an icon based on a file extension, with extensive customization options for colors, gradients, folds, and labels. The library is currently on version 1.6.0 and shows an active development cadence, with several minor releases supporting newer React versions (up to React 19) and improving bundle size through dependency swaps (e.g., `colord` replacing `tinycolor2`). Key differentiators include its rich set of customizable props, an exported `defaultStyles` object for common file extensions, and the use of modern React features like `React.useId` for unique ID generation, ensuring efficient and accessible icon rendering.
Common errors
-
TypeError: (0 , react_file_icon__WEBPACK_IMPORTED_MODULE_0__.FileIcon) is not a function
cause Attempting to import `FileIcon` as a default export or using CommonJS `require()` syntax after version 1.0.0.fixUse a named import: `import { FileIcon } from 'react-file-icon';` -
Property 'size' does not exist on type 'IntrinsicAttributes & FileIconProps'.
cause Using the `size` prop on the `FileIcon` component, which was removed in version 1.0.0.fixRemove the `size` prop and control the icon's dimensions by styling its parent container. -
TypeError: Cannot read properties of undefined (reading 'xyz')
cause Attempting to access a style property from `defaultStyles` for an extension (`xyz` in this example) that is not defined in the `defaultStyles` object.fixVerify the extension name matches a key in `defaultStyles` or provide custom styles directly to the `FileIcon` component for unsupported extensions.
Warnings
- breaking The `FileIcon` component is no longer the default export. It must now be imported as a named export.
- breaking The `size` prop has been removed. Icons now inherently scale to 100% width of their container. Developers should control the icon size by adjusting the dimensions of the parent element.
- gotcha Version 1.5.0 replaced `lodash.uniqueid` with `React.useId` for generating unique IDs, with a fallback for older React versions. While a fallback exists, optimal performance and adherence to modern React practices benefit from using React versions that natively support `React.useId` (React 18+).
Install
-
npm install react-file-icon -
yarn add react-file-icon -
pnpm add react-file-icon
Imports
- FileIcon
import FileIcon from 'react-file-icon'; const FileIcon = require('react-file-icon');import { FileIcon } from 'react-file-icon'; - defaultStyles
const { defaultStyles } = require('react-file-icon');import { defaultStyles } from 'react-file-icon'; - FileIconProps
import type { FileIconProps } from 'react-file-icon';
Quickstart
import React from 'react';
import { FileIcon, defaultStyles } from 'react-file-icon';
const MyFileDisplay = () => {
return (
<div style={{ display: 'flex', gap: '20px', padding: '20px' }}>
<div style={{ width: '100px', height: '100px' }}>
<FileIcon extension="pdf" {...defaultStyles.pdf} />
</div>
<div style={{ width: '100px', height: '100px' }}>
<FileIcon extension="docx" {...defaultStyles.docx} />
</div>
<div style={{ width: '100px', height: '100px' }}>
<FileIcon extension="png" {...defaultStyles.png} />
</div>
<div style={{ width: '100px', height: '100px' }}>
<FileIcon
extension="js"
type="code"
color="#f7df1e"
glyphColor="#333"
labelColor="#333"
labelTextColor="white"
labelUppercase
/>
</div>
</div>
);
};
export default MyFileDisplay;