1. What is The essence is the component instance returned by 2. How to use There are three ways to create
Passing in a string Just need to add the class MyComponent extends React.Component { constructor(props) { super(props); this.myRef = React.createRef(); } render() { return <div ref="myref" />; } } The way to access the current node is as follows: this.refs.myref.innerHTML = "hello"; Incoming Object class MyComponent extends React.Component { constructor(props) { super(props); this.myRef = React.createRef(); } render() { return <div ref={this.myRef} />; } } When const node = this.myRef.current; Passing in a function When class MyComponent extends React.Component { constructor(props) { super(props); this.myRef = React.createRef(); } render() { return <div ref={element => this.myref = element} />; } } To get the const node = this.myref Passing in hooks Create a function App(props) { const myref = useRef() return ( <> <div ref={myref}></div> </> ) } Getting the const node = myref.current; In the above three cases, Note that you cannot use the 3. Application Scenarios In some cases, we update components by using Excessive use of For example, avoid exposing But
This is the end of this article about understanding React refs. For more information about understanding React refs, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to install suPHP for PHP5 on CentOS 7 (Peng Ge)
>>: Solution to the Multiple primary key defined error in MySQL
When the scroll bar is pulled down, the floating ...
Table of contents Introduction Get started A brie...
In this blog, we will discuss ten performance set...
Port Mapping Before the Docker container is start...
What is a sticky footer layout? Our common web pa...
Suppose there is a table: reward (reward table), ...
INSERT INTO hk_test(username, passwd) VALUES (...
ClickHouse is an open source column-oriented DBMS...
Abstract: This article will demonstrate how to se...
Requirements: Remove HTTP response headers in IIS...
Using padding-top percentage can achieve a fixed ...
List of HTML tags mark type Name or meaning effec...
Preface There are often some articles on the Inte...
The order in which the browser loads and renders H...
<br />Although there are many web page creat...