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
1. <body background=image file name bgcolor=co...
How to modify the style of the el-select componen...
1 Pull the image from hup docker pull nginx 2 Cre...
When switching users in the docker container, it ...
Here is a case that front-end developers must kno...
<br />Original text: http://www.mikkolee.com...
Table of contents Preface 1. What is a lock? 2. L...
This article example shares the specific code of ...
Preface Docker can configure environment variable...
Problem description: After executing docker run -...
With a lot of CSS experience as a web designer, we...
Table of contents Preface What is a filter How to...
Table of contents Preface 1. Use global unified o...
Preface Before talking about covering index, we m...
Table of contents 1. Install Docker 2. Pull the J...