Screen.Recording.2024-03-31.at.18.27.10.mov
This repository provides an example of how to utilize ref
in React for managing references to DOM elements.
Refs in React provide a way to directly access DOM elements or React components created in the render method. They offer a way to interact with DOM elements imperatively, which can be useful for tasks like managing focus, triggering animations, or integrating with third-party DOM libraries.
To use ref
in your React project, follow these steps:
-
Create Refs: You can create a ref using the
useRef()
hook. For example:import React, { useRef } from 'react'; const MyComponent = () => { const myRef = useRef(null); // Now `myRef` can be used to refer to a DOM element or a React component };
-
Attach Refs to Elements or Components: Use the
ref
attribute to attach the created ref to a DOM element or a React component. For example:<input ref={myRef} />
or
<MyComponent ref={myRef} />
-
Accessing Refs: You can access the current value of the ref using the
.current
property. For example:const node = myRef.current;
-
Manipulating DOM with Refs: Once you have a reference to a DOM element, you can manipulate it directly. For example:
myRef.current.focus();
Here's a simple example demonstrating the usage of ref
in a React component:
import React, { useRef } from 'react';
const MyComponent = () => {
const inputRef = useRef(null);
const focusInput = () => {
inputRef.current.focus();
};
return (
<div>
<input ref={inputRef} />
<button onClick={focusInput}>Focus Input</button>
</div>
);
};
export default MyComponent;
- Direct Access to DOM: Refs provide direct access to DOM elements, allowing for imperative actions.
- Integration with Third-party Libraries: Refs enable seamless integration with third-party DOM libraries that might not follow the React component model.
- Performance Optimization: Refs can be used to store references to elements or values that don't trigger re-renders, improving performance.