react-file-viewer

repository·master·Indexed 20 days ago

https://github.com/plangrid/react-file-viewer

An extendable React component for rendering various file formats in a web browser, including documents (pdf, docx, csv, xlsx), images (jpg, jpeg, gif, bmp, png), audio (mp3), video (webm, mp4), and BIM (wexbim). The library provides a FileViewer component that accepts fileType and filePath props and allows for custom error and unsupported file type components.

Tokens
1.3K
Snippets
6
Records
8
Agent score
68%

What's inside react-file-viewer

  1. Install react-file-viewer

    master

    Install the latest version for React 16+ using npm. If you are using a version of React older than 16, you must install version 0.5.0 specifically.

    # For React 16+
    npm install react-file-viewer
    
    # For React < 16
    npm install react-file-viewer@0.5.0
  2. Extend the file viewer with new file types

    master

    To add support for a new file type (e.g., .rtf), you must create a 'driver' component and register it within the library's core logic:

    1. Create a driver component (a component capable of rendering that file type).
    2. Add the driver component to src/components/drivers.
    3. Import the new component into file-viewer.jsx.
    4. Add a new case to the getDriver method in file-viewer.jsx to return your component.
    // Inside file-viewer.jsx -> getDriver method
    case 'rtf':
      return RtfViewer;
  3. Use the FileViewer component

    master

    The FileViewer component is the primary interface for displaying files. It requires fileType and filePath props. You can also provide custom components for error states or unsupported file types, and an onError callback for error handling.

    import React, { Component } from 'react';
    import FileViewer from 'react-file-viewer';
    import { CustomErrorComponent } from 'custom-error';
    
    const file = 'http://example.com/image.png';
    const type = 'png';
    
    class MyComponent extends Component {
      render() {
        return (
          <FileViewer
            fileType={type}
            filePath={file}
            errorComponent={CustomErrorComponent}
            onError={this.onError}/>
        );
      }
    
      onError(e) {
        // Handle error (e.g., logging)
        console.error(e);
      }
    }
  4. Reference: FileViewer props

    master

    The following props are available for the FileViewer component:

    fileType (string): type of resource to be shown (e.g., 'png', 'pdf', 'mp4').
    filePath (string): the url of the resource to be shown.
    errorComponent (react element, optional): A component to render in case of error instead of the default.
    errorComponent (function, optional): onError callback function called when there is an error fetching or rendering.
    unsupportedComponent (react element, optional): A component to render in case the file format is not supported.
  5. Import the FileViewer component

    master

    The react-file-viewer package exports the main FileViewer component as its primary entry point. You can import it directly from the package root.

    const FileViewer = require('react-file-viewer');
    // or if using ES modules in your environment:
    // import FileViewer from 'react-file-viewer';
  6. Configure FileViewer props

    master

    The FileViewer component accepts the following props:

    PropTypeRequiredDescription
    fileTypestringYesThe extension/type of the file (e.g., 'pdf', 'csv', 'xlsx', 'jpg', 'mp4').
    filePathstringYesThe URL or path to the file resource.
    onErrorfunctionNoCallback function triggered when an error occurs. Defaults to () => null.
    errorComponentelementNoA custom React element to render when an error occurs.
    unsupportedComponentelementNoA custom React element to render when the fileType is not supported.