Interviewer: Tell me about your understanding of React refs? Application scenario?
1. Refs
Refs
In computers, it is called Resilient File System (English: Resilient File System, ReFS for short)
React
in Refs
provides a way that allows us to access DOM
nodes or elements render
created in the methodReact
The essence is ReactDOM.render()
the returned component instance, if it is a rendered component, it will return the component instance, if it is rendered, dom
it will return the specific dom
node
2. How to use
There are three forms created ref
:
- Pass in a string, and get the corresponding element through the format of the string passed in this.refs.
- Pass in the object, the object is created by React.createRef(), and the current attribute in the created object is obtained when using it, which is the corresponding element
- Pass in a function, which will call back when the DOM is mounted. This function will pass in an element object, which can be saved by itself. When using it, just get the previously saved element object directly.
- Pass in the hook, the hook is created by useRef(), and the current attribute of the generated hook object is the corresponding element when used
incoming string
ref
Only need to attribute in the corresponding element or component
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
refs
By React.createRef()
creating and then ref
adding attributes to React
the element, as follows:
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
}
render() {
return <div ref={this.myRef} />;
}
}
When ref
a is passed to render
an element in a , a reference to the node can be accessed in the attribute ref
of thecurrent
const node = this.myRef.current;
pass in function
When ref
passed in as a function, during the rendering process, the callback function parameter will pass in an element object, and then save the object through the instance
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
}
render() {
return <div ref={element => this.myref = element} />;
}
}
To get ref
an object, you only need to pass through the previously stored object
const node = this.myref
incoming hook
By useRef
creating a ref
, the overall usage is React.createRef
consistent with
function App(props) {
const myref = useRef()
return (
<>
<div ref={myref}></div>
</>
)
}
Obtaining ref
properties is also through the properties hook
of the objectcurrent
const node = myref.current;
The above three cases are ref
attributes used on native HTML
elements. If ref
the set component is a class component, ref
the object receives the mounted instance of the component
Note that properties cannot be used on function components ref
because they do not have instances
3. Application scenarios
In some cases, we will refs
update the component by using it, but this method is not recommended. In more cases, we use props
the state
method of re-rendering child elements
Excessive use will expose refs
the instance or structure of the component , which violates the principle of component encapsulationDOM
For example, avoid exposing and methods in Dialog
components , it is better to pass propsopen()
close()
isOpen
But the following scenarios are refs
very useful:
- Focus control, content selection, and control of Dom elements
- Content setting and media playback of Dom elements
- Operations on Dom elements and operations on component instances
- Integrate third-party DOM libraries
references
- https://zh-hans.reactjs.org/docs/refs-and-the-dom.html
- https://segmentfault.com/a/1190000020842342