All files / react-preview-file/src filePreview.tsx

88.89% Statements 32/36
56.25% Branches 9/16
84.62% Functions 11/13
94.12% Lines 32/34
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46  1x 1x     1x 1x 1x 1x     1x 1x 1x 1x   3x   1x 1x 1x 1x 1x 1x     1x 3x 3x   1x 1x   1x 4x   1x 2x   1x 4x 4x   1x   1x
import * as React from 'react';
import {Component, ReactNode} from 'react';
 
export interface FilePreviewProps {
  file: File;
  children: (preview: string) => ReactNode;
}
 
export class FilePreview extends Component <FilePreviewProps> {
  preview: string;
 
  componentWillReceiveProps({file: newFile}: FilePreviewProps) {
    const {file} = this.props;
 
    if (file !== newFile) {
      this.revoke();
      this.createPreview(newFile);
    }
  }
 
  componentWillMount() {
    consEt {file} = this.props;
 
    this.createPreview(file);
  }
 
  componentWillUnmount() {
    this.revoke();
  }
 
  private createPreview(file: File) {
    this.preview = URL.createObjectURL(file);
  }
 
  private revoke() {
    URL.revokeObjectURL(this.preview);
  }
 
  render() {
    const {children} = this.props;
 
    return children(this.preview);
  }
}
 
export default FilePreview;