Several ways to introduce pictures in react projects

Several ways to introduce pictures in react projects

The img tag introduces the image

Because react actually renders the page through the js reader function, you cannot import the image by directly writing src="path"

We can import images just like importing modules

import img from './../../../../asset/img/user.png'

Need to be introduced in the following way

<img src={require('../images/picture.png')} alt="label"/>

Background image introduction

1 The first is to create a new CSS file in a regular way, and then you can directly write the CSS syntax

.img {
   background: url('../images/picture.png') 0 0 no-repeat;
}

2 The second way is to introduce it in the react component through variables, and then directly assign the variables to the img tag

// Import image file import bg from '../images/bg.png'
// Define a style object by string concatenation const imgStyle = {
  width: '100%',
  height: '500px',
  backgroundImage: 'url(' + bg + ')',
  backgroundPosition: 'center 0',
  backgroundSize: '2045px 472px',
  backgroundRepeat: 'no-repeat'
}
class Home extends Component {
 constructor () {
  super (props)
 }
 render() {
  //Finally assign the variable directly to the tag <div style="imgStyle">
   ...
  </div>
 }
}

require

We can also wrap the relative path with require and assign it directly to src, just like in vue.

<img width="100" height="100" src={require('./../../../../asset/img/user.png')} alt="" className={'user-img'}/>

**Note:** There is a problem here because there are some minor differences due to different versions of the file-loader library. In higher versions of the file-loader library, esModule defaults to true, and require returns an ES module instead of a string path. The default attribute of this ES module is a string path, so it should be written like this:

<img width="100" height="100" src={require('./../../../../asset/img/user.png').default} alt="" className={'user-img'}/>

We should not worry about the version of our file-loader. When we use require directly and still cannot display the image normally, we can add .default after require.

This concludes this article about several ways to introduce images into react projects. For more relevant content about introducing images into react, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A brief introduction to React
  • How to introduce scss into react project
  • How to introduce Angular components in React
  • React introduces container components and display components to Vue

<<:  A brief discussion on the implementation of MySQL's limit paging optimization solution

>>:  Tutorial diagram of installing CentOS and Qt in Vmware virtual machine

Recommend

How to use shell scripts in node

background During development, we may need some s...

Practical notes on installing Jenkins with docker-compose

Create a Directory cd /usr/local/docker/ mkdir je...

MySQL 5.6.27 Installation Tutorial under Linux

This article shares the installation tutorial of ...

Install and build a server environment of PHP+Apache+MySQL on CentOS

Yum (full name Yellow dog Updater, Modified) is a...

Installation tutorial of MySQL 5.7.17 zip package version under win10

The installation tutorial of mysql5.7.17 is share...

Detailed Example of MySQL curdate() Function

MySQL CURDATE Function Introduction If used in a ...

Deep understanding of line-height and vertical-align

Several concepts Line box: A box that wraps an in...

Detailed explanation of incompatible changes of components in vue3

Table of contents Functional Components How to wr...

Solve the problem of Navicat for Mysql connection error 1251 (connection failed)

Because what I wrote before was not detailed enou...

Vue project @change multiple parameters to pass multiple events

First, there is only one change event. changeleve...

Simple steps to implement H5 WeChat public account authorization

Preface Yesterday, there was a project that requi...