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:
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 ComponentIn 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 Componentsfunction 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:
|
<<: Alibaba Cloud ESC Server Docker Deployment of Single Node Mysql
>>: Detailed Example of MySQL curdate() Function
Introduction The meta tag is an auxiliary tag in ...
This article is based on the Windows 10 system en...
Building an image is a very important process in ...
Demand scenario: The existing PXC environment has...
GitHub address, you can star it if you like it Pl...
Table of contents 1. Project folder structure 1. ...
1. Create a new UI project First of all, our UI i...
Mini Program Data Cache Related Knowledge Data ca...
The difference between inline elements and block-...
Table of contents 1. Required attributes 1. name ...
Date-type single-row functions in MySQL: CURDATE(...
Table of contents Overview console.log console.in...
MySQL prompts the following error I went to "...
Table of contents Preface π Start researching π±βπ...
Table of contents 1. Overview 2. Application Exam...