Anaïs Saez
Anaïs Saez

Reputation: 1339

Using React-file-viewer

I'm trying to use React-file-viewer. Npm tutorial is here But I have an error in the console : "you may need an appropriate loader to handle this file type" This is my code :

import FileViewer from 'react-file-viewer';
import { CustomErrorComponent } from 'custom-error';
const file = 'http://example.com/image.png'
const type = 'png'


 onError = (e) => {
    logger.logError(e, 'error in file-viewer');
 }

 <FileViewer
 fileType={type}
 filePath={file}
 errorComponent={CustomErrorComponent}
 onError={this.onError}/>

I specify, i have babel-preset-es2015 and i use it

How can I do ? Thank you

Upvotes: 6

Views: 10601

Answers (5)

Mohammad Atiour Islam
Mohammad Atiour Islam

Reputation: 5708

You need to import the file using require.

// import FileViewer from "react-file-viewer";
const FileViewer = require('react-file-viewer');

After that if you are getting any error like Module not found: Can't resolve 'console'. you can run

npm install console

Upvotes: 1

Harsha Pk
Harsha Pk

Reputation: 11

You need to import logger from Logging library, something like this:

import logger from 'logging-Lib';

see more: https://www.npmjs.com/package/react-file-viewer

Upvotes: 1

dk-na
dk-na

Reputation: 141

Make sure you have installed the following presets and plugins, as listed in node-modules/react-file-viewer/.babelrc file:

{
  "presets": [
    "react",
    "es2015",
    "stage-0"
  ],
  "plugins": [
    "transform-class-properties",
    "transform-es2015-classes",
    "transform-es2015-object-super",
    "transform-runtime"
  ]
}

Assuming you already have the react and es2015 in your project, the npm command will be:

npm install --save-dev babel-preset-stage-0 \
    babel-plugin-transform-class-properties \
    babel-plugin-transform-es2015-classes \
    babel-plugin-transform-es2015-object-super \
    babel-plugin-transform-runtime 

Upvotes: 1

js_248
js_248

Reputation: 2122

You have to include babel in webpack config as

loaders: [
      {test: /\.js$/, include: path.join(__dirname, 'src'), loaders: ['babel']},
      { test: /\.jsx$/, exclude: /node_modules/, loader: 'babel-loader' }]

As you are using

onError = (e) => {
    logger.logError(e, 'error in file-viewer');
 }

which is es6 syntax.To make it browser compatible you have to add

{test: /\.js$/, include: path.join(__dirname, 'src'), loaders: ['babel']}

Upvotes: 1

Ana&#239;s Saez
Ana&#239;s Saez

Reputation: 1339

module: {
        loaders: [
            // .ts(x) files should first pass through the Typescript loader, and then through babel
            { test: /\.tsx?$/, loaders: ['babel', 'ts-loader'] },
            { test: /\.css$/, loaders: ['style', 'css-loader'] },
            { test: /\.scss$/, loaders: ['style', 'css-loader?modules&importLoaders=1&localIdentName=[local]-[hash:base64:5]', 'postcss-loader', 'sass'] },
            { test: /\.(png|svg|gif|jpg|jpeg)$/, loaders: [ 'url-loader', 'image-webpack?bypassOnDebug'] },
            { test: /\.(eot|woff|ttf|woff2)$/, loader: "file?name=[name].[ext]" }


        ]
    }

Upvotes: 1

Related Questions