Table of Contents
Requirements
Ideas
Complete code
Problems encountered during the process
Home Web Front-end Front-end Q&A How to set div height in react

How to set div height in react

Jan 06, 2023 am 10:19 AM
react.js

How to set div height in react: 1. Implement div height through css; 2. Declare an object C in state and store the style of the change button in the object, then get A and reset C Just click "marginTop" in it.

How to set div height in react

The operating environment of this tutorial: Windows 10 system, react18.0.0 version, Dell G3 computer.

How to set div height in react?

react dynamically sets element height

Use react to implement Dynamic modification of style (this method is not recommended unless there is no Other options)

As shown in the figure below:

How to set div height in react

Requirements

The replacement button in the implementation diagram follows In the lower right corner of the image, when the height of the image changes, the position of the change button also changes.

Ideas

1. Implement through css (there is no solution for the time being)
2. Use the characteristics of react data-driven view
2.1 Data: declare one in state Object C, which stores the style of the change button.
2.2 Operation: Let A = picture height, B = change button height. After the image is loaded, obtain A and set the marginTop in C again. (This idea only needs to change the data and does not need to consider the view layer)

Complete code

construct

this.state = {
  updateBtnStyle :{
    fontSize: "12px",marginLeft:'20px',marginTop:'300px',
  }
}
//在class中定义的函数,如果需要使用到全局this,需要此操作
this.loading = this.loading.bind(this)
Copy after login

Declare the operation function loading

loading(){
    const imgHeight =  document.getElementById('facePhoto').height
    console.log('图片的高度',document.getElementById('facePhoto')?.height)
    if(imgHeight === Number.parseInt(this.state.updateBtnStyle.marginTop)+24) return
    else{
      this.setState({
        updateBtnStyle:{
          marginTop:document.getElementById('facePhoto')?.height-24+'px'
        }
      })
    }
}
Copy after login

View layer Pseudocode

<Item label="人脸照片" style={{ position: "relative" }} required>
    <img id={&#39;facePhoto&#39;} src={photo} alt=""  width="300" onLoad={this.loading} style={{float:&#39;left&#39;}}/>
    <Button size="small" style={updateBtnStyle}
      onClick={() => StateManage.set(updatePhotoProps, { visible: true })}>更换</Button>
 </Item>
Copy after login

Problems encountered during the process

1. Obtain the height of the image:
1.1 Acquisition time: You need to wait for the image to be loaded to complete to obtain its height before it is effective, so you need to use The onLoad event of img ensures that the image has been loaded when the height of the image is obtained.
1.2 How to obtain: The width and height of the picture are set through the attributes height and width, not through style, so the method for obtaining the height is =document.getElementById('xxx').height
2. The declared function does not have Binding this makes it impossible to use setState

Recommended learning: "react video tutorial"

The above is the detailed content of How to set div height in react. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to call the method of child component in React parent component How to call the method of child component in React parent component Dec 27, 2022 pm 07:01 PM

Calling method: 1. Calls in class components can be implemented by using React.createRef(), functional declaration of ref or props custom onRef attribute; 2. Calls in function components and Hook components can be implemented by using useImperativeHandle or forwardRef to throw a child Component ref is implemented.

How to debug React source code? Introduction to debugging methods using multiple tools How to debug React source code? Introduction to debugging methods using multiple tools Mar 31, 2023 pm 06:54 PM

How to debug React source code? The following article will talk about how to debug React source code under various tools, and introduce how to debug the real source code of React in contributors, create-react-app, and vite projects. I hope it will be helpful to everyone!

In-depth understanding of React's custom Hooks In-depth understanding of React's custom Hooks Apr 20, 2023 pm 06:22 PM

React custom Hooks are a way to encapsulate component logic in reusable functions. They provide a way to reuse state logic without writing classes. This article will introduce in detail how to customize encapsulation hooks.

How to set div height in react How to set div height in react Jan 06, 2023 am 10:19 AM

How to set the div height in react: 1. Implement the div height through CSS; 2. Declare an object C in the state and store the style of the change button in the object, then get A and reset the "marginTop" in C. That is Can.

Why React doesn't use Vite as the first choice for building apps Why React doesn't use Vite as the first choice for building apps Feb 03, 2023 pm 06:41 PM

Why doesn’t React use Vite as the first choice for building applications? The following article will talk to you about the reasons why React does not recommend Vite as the default recommendation. I hope it will be helpful to everyone!

7 great and practical React component libraries (shared under pressure) 7 great and practical React component libraries (shared under pressure) Nov 04, 2022 pm 08:00 PM

This article will share with you 7 great and practical React component libraries that are often used in daily development. Come and collect them and try them out!

10 practical tips for writing cleaner React code 10 practical tips for writing cleaner React code Jan 03, 2023 pm 08:18 PM

This article will share with you 10 practical tips for writing simpler React code. I hope it will be helpful to you!

[Translation] Refactoring React components using custom hooks [Translation] Refactoring React components using custom hooks Jan 17, 2023 pm 08:13 PM

I often hear people talk about React function components, mentioning that function components will inevitably become larger and more logically complex. After all, we wrote the component in "a function", so you have to accept that the component will expand and the function will continue to expand.

See all articles