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

Simple example of adding and removing HTML nodes

Simple example of adding and removing HTML nodes ...

Detailed explanation of the use of stat function and stat command in Linux

stat function and stat command Explanation of [in...

Window.name solves the problem of cross-domain data transmission

<br />Original text: http://research.microso...

Some tips for using less in Vue projects

Table of contents Preface 1. Style penetration 1....

js regular expression lookahead and lookbehind and non-capturing grouping

Table of contents Combining lookahead and lookbeh...

Various transformation effects of HTML web page switching

<META http-equiv="Page-Enter" CONTENT...

How to use Vue3 mixin

Table of contents 1. How to use mixin? 2. Notes o...

Specific example of MySQL multi-table query

1. Use the SELECT clause to query multiple tables...

Detailed explanation of HTML page header code example

Knowledge point 1: Set the base URL of the web pa...

How to Check Memory Usage in Linux

When troubleshooting system problems, application...

Detailed example of reading speed of js objects

1. Accessing literals and local variables is the ...

MySql Installer 8.0.18 Visual Installation Tutorial with Pictures and Text

Table of contents 1. MySQL 8.0.18 installation 2....

JS+Canvas draws a lucky draw wheel

This article shares the specific code of JS+Canva...

Why the explain command may modify MySQL data

If someone asked you whether running EXPLAIN on a...