Skip to content

hashanCB/Accordion

Repository files navigation

Using Refs in React

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.

What are Refs?

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.

Usage

To use ref in your React project, follow these steps:

  1. 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
    };
  2. 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} />
  3. Accessing Refs: You can access the current value of the ref using the .current property. For example:

    const node = myRef.current;
  4. Manipulating DOM with Refs: Once you have a reference to a DOM element, you can manipulate it directly. For example:

    myRef.current.focus();

Example

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;

Benefits of Using Refs

  • 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published