DEV Community

Cover image for React Ref: Everything You Need to Know in Simple Terms
Itunu Lamina
Itunu Lamina

Posted on • Originally published at itunulamina.com

React Ref: Everything You Need to Know in Simple Terms

So, imagine you're at a party and you see this cute person across the room that you want to talk to. But, there's a problem: they're surrounded by a bunch of other people and you can't get to them.

In this article, we will explore everything you need to know about React ref, including what it is, how it works, and how you can use it to enhance your React applications.

What is React Ref?

React Ref is a feature in React that provides a way to access and manipulate the DOM elements directly. Refs allow developers to refer to a specific element or component in their code, even when that element is not in the current scope of the component.

Well, React Ref is like having a VIP pass that lets you skip the line and go straight to the cute person. In this case, the cute person is an element in your React component, and the other people are all the other elements surrounding it.

How does React Ref work?

React Ref works by creating a reference to a specific element in the DOM. This reference is created using the ref attribute in the JSX code, which is then attached to the element you want to refer to.

Using React Ref

But, how do you get this VIP pass? It's easy! You just need to create a reference to the element you want to access using the useRef() hook in a component:

import React, { useRef } from 'react';

function MyComponent() {
  const myRef = useRef(null);

  return (
    <div ref={myRef}>
      Hello, world!
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Now, you have a reference to the div element and you can access its properties and methods by calling myRef.current. For example, if you want to change the color of the text to blue, you can do this:

myRef.current.style.color = 'blue';
Enter fullscreen mode Exit fullscreen mode

Forwarding React Ref

But, what if you want to pass this VIP pass to another component? Well, you can use the forwardRef() function to forward the reference from the parent component to the child component. It's like giving your friend your VIP pass so they can also talk to the cute person!

import React, { useRef, forwardRef } from 'react';

const ChildComponent = forwardRef((props, ref) => (
  <div ref={ref}>
    I'm the child component!
  </div>
));

function ParentComponent() {
  const myRef = useRef(null);

  return (
    <div>
      <ChildComponent ref={myRef} />
      <button onClick={() => myRef.current.style.color = 'red'}>
        Make it red!
      </button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Now, you have access to the div element in the child component from the parent component. And, when you click the button, the text color will change to red!

Conclusion

So, that's React Ref. It's like having a VIP pass to the cute person you want to talk to at a party, and being able to pass it to your friends too!

In this article, we have explored everything you need to know about React Ref, including what it is, how it works, and how to use it. By using React Ref in your applications, you can enhance the user experience and create more dynamic and responsive UIs.

Keep building! 🚀

Top comments (0)