Detailed explanation of the use of Refs in React's three major attributes

Detailed explanation of the use of Refs in React's three major attributes

Refs is a method used in React to obtain nodes when obtaining some state values ​​in a JSX component or a DOM. In React's official explanation, its scope of application is as follows:

  • Manage focus, text selection, or media playback.
  • Triggers a forced animation.
  • Integrate third-party DOM libraries.

React documentation repeatedly emphasizes that you should not overuse refs, so when we can use DOM native objects to solve it, try not to use refs. According to the previous writing method, first give the writing method of refs in class components and function components

Class Component

In a class, there are three ways to use refs. The most commonly used one is callback.

//Directly define refs, deprecated class App extends React.PureComponent{
    changeInput = ()=>{
        const {input} = this.refs
    }
    render() {
        return (
            <div>
                <input type="text" placeholder={"please input your value"} onBlur={this.changeInput} ref={"input"}/>
            </div>
        )
    }
}

//Use in callback form class App extends React.PureComponent{
    changeInput = ()=>{
        console.log(this.inputRef);
    }
    render() {
        return (
            <div>
                <input type="text" placeholder={"please input your value"} onBlur={this.changeInput} ref={(el)=>{this.inputRef = el}}/>
            </div>
        )
    }
}

//Use createRef
class App extends React.PureComponent{
    inputRef = React.createRef()
    changeInput = ()=>{
        console.log(this.inputRef.current);
    }
    render() {
        return (
            <div>
                <input type="text" placeholder={"please input your value"} onBlur={this.changeInput} ref={this.inputRef}/>
            </div>
        )
    }
}

The above are the three ways to write Ref of class components

Functional Components

function App(){
    const inputRef = useRef("")
    return (
        <div>
            <input type="text" placeholder={"please input your value"} ref={inputRef}/>
        </div>
    )
}

Use a useRef to complete the code directly

Interview FAQ: What is the role of refs in React?

Refs are handles that React provides us with safe access to DOM elements or component instances. In a class component, React treats the first argument in the ref attribute as a handle in the DOM. In the function component, react can also get ref using the hooks api useRef (the useRef feature is often used in hooks, that is, the stored data is not refreshed as the component is refreshed, so as to write some constant amounts)

The above is the detailed content of the detailed explanation of the use of Refs, one of the three major attributes of React. For more information about Refs, one of the three major attributes of React, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • An in-depth introduction to React refs
  • Summary of some common uses of refs in React
  • Tutorial on using refs in React
  • Detailed explanation of the use of React component refs
  • Do you know the Refs attribute in React?

<<:  Alibaba Cloud ESC Server Docker Deployment of Single Node Mysql

>>:  Detailed Example of MySQL curdate() Function

Recommend

js implements axios limit request queue

Table of contents The background is: What will ha...

Detailed explanation of various types of image formats such as JPG, GIF and PNG

Everyone knows that images on web pages are genera...

What is BFC? How to clear floats using CSS pseudo elements

BFC Concept: The block formatting context is an i...

Docker memory monitoring and stress testing methods

The Docker container that has been running shows ...

Detailed explanation of the use of CSS pointer-events attribute

In front-end development, we are in direct contac...

JS implements click drop effect

js realizes the special effect of clicking and dr...

How to use js to determine whether a file is utf-8 encoded

Conventional solution Use FileReader to read the ...

Solution for adding iptables firewall policy to MySQL service

If your MySQL database is installed on a centos7 ...

Detailed steps for installing MinIO on Docker

Table of contents 1. Check whether the docker env...

Implementing calculator functions with WeChat applet

This article is a simple calculator written using...

XHTML Getting Started Tutorial: Simple Web Page Creation

Create your first web page in one minute: Let'...

Summary of various methods of MySQL data recovery

Table of contents 1. Introduction 2. Direct recov...

CentOS8 - bash: garbled characters and solutions

This situation usually occurs because the Chinese...

Summary of Vue watch monitoring methods

Table of contents 1. The role of watch in vue is ...