V
V
Vadim Semennikov2019-06-14 15:13:28
JavaScript
Vadim Semennikov, 2019-06-14 15:13:28

How to implement image loading with webpack loader?

Hello! Trying to smoke React.
There is a task: The image must be imported into the component and processed by the webpack loader.
Can you please explain how to implement this? What to do with him? Is there an example
Thank you

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-06-14
@VadimRosh

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.(png|jpe?g|gif)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: '[path][name].[ext]',
            },
          },
        ],
      },
    ],
  },
};

file-loader
An example of using images in code:
import React from 'react';
import image from './image.png';

const Example = () => (
  <img src={image} />
);

or:
import React from 'react';

const Example = () => (
  <img src={require('./image.png')} />
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question